using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI;

using AForge.Math;
using AForge.Imaging;
using AForge.Imaging.Filters;
using MatrixDataStructures;
using CommonNamespace;
using ImageProcessingNamespace;
using Magnifier;

    /// <summary>
    /// Summary description for ImageDoc.
    /// </summary>
    public class ImageDoc : Content
    {
        Int16[, ,] datamatrix;
        Int16[, ,] tempdatamatrix;

        public float max, min; //used for contrast_stretch

        MatrixDataStructures.ColorMatrix CMatrix;
        public bool IsColorMatrixLoaded = false;
        int x = 0, y = 0, z = 0;

        private System.Drawing.Bitmap backup = null;//, image=null;
        //public ClassesYVDH.AnnotatedsRGBImage image = null;
        public System.Drawing.Bitmap image = null;
        private string fileName = null;
        private int width;
        private int height;
        public float zoom = 1;
        private IDocumentsHost host = null;

        private bool cropping = false;
        private bool dragging = false;
        private Point start, end, startW, endW;

        public bool ROIEnabled = false;

        private System.Windows.Forms.MainMenu mainMenu;
        private System.Windows.Forms.MenuItem imageItem;
        private System.Windows.Forms.MenuItem filtersItem;
        private System.Windows.Forms.MenuItem cloneImageItem;
        private System.Windows.Forms.MenuItem rotateColorFiltersItem;
        private System.Windows.Forms.MenuItem invertColorFiltersItem;
        private System.Windows.Forms.MenuItem sepiaColorFiltersItem;
        private System.Windows.Forms.MenuItem grayscaleColorFiltersItem;
        private System.Windows.Forms.MenuItem menuItem1;
        private System.Windows.Forms.MenuItem menuItem2;
        private System.Windows.Forms.MenuItem backImageItem;
        private System.Windows.Forms.MenuItem menuItem4;
        private System.Windows.Forms.MenuItem menuItem5;
        private System.Windows.Forms.MenuItem menuItem6;
        private System.Windows.Forms.MenuItem menuItem7;
        private System.Windows.Forms.MenuItem z10ImageItem;
        private System.Windows.Forms.MenuItem z25ImageItem;
        private System.Windows.Forms.MenuItem z50ImageItem;
        private System.Windows.Forms.MenuItem z75ImageItem;
        private System.Windows.Forms.MenuItem z100ImageItem;
        private System.Windows.Forms.MenuItem z150ImageItem;
        private System.Windows.Forms.MenuItem z200ImageItem;
        private System.Windows.Forms.MenuItem z400ImageItem;
        private System.Windows.Forms.MenuItem z500ImageItem;
        private System.Windows.Forms.MenuItem menuItem8;
        private System.Windows.Forms.MenuItem zoomInImageItem;
        private System.Windows.Forms.MenuItem zoomOutImageItem;
        private System.Windows.Forms.MenuItem menuItem11;
        private System.Windows.Forms.MenuItem zoomFitImageItem;
        private System.Windows.Forms.MenuItem colorFiltersItem;
        private System.Windows.Forms.MenuItem binaryFiltersItem;
        private System.Windows.Forms.MenuItem thresholdBinaryFiltersItem;
        private System.Windows.Forms.MenuItem thresholdCarryBinaryFiltersItem;
        private System.Windows.Forms.MenuItem floydBinaryFiltersItem;
        private System.Windows.Forms.MenuItem morphologyFiltersItem;
        private System.Windows.Forms.MenuItem dilatationMorphologyFiltersItem;
        private System.Windows.Forms.MenuItem convolutionFiltersItem;
        private System.Windows.Forms.MenuItem meanConvolutionFiltersItem;
        private System.Windows.Forms.MenuItem blurConvolutionFiltersItem;
        private System.Windows.Forms.MenuItem sharpenConvolutionFiltersItem;
        private System.Windows.Forms.MenuItem edgesConvolutionFiltersItem;
        private System.Windows.Forms.MenuItem levelsFiltersItem;
        private System.Windows.Forms.MenuItem flipImageItem;
        private System.Windows.Forms.MenuItem mirrorItem;
        private System.Windows.Forms.MenuItem rotateImageItem;
        private System.Windows.Forms.MenuItem menuItem10;
        private System.Windows.Forms.MenuItem cropImageItem;
        private System.Windows.Forms.MenuItem menuItem3;
        private System.Windows.Forms.MenuItem menuItem9;
        private System.Windows.Forms.MenuItem customMorphologyFiltersItem;
        private System.Windows.Forms.MenuItem menuItem12;
        private System.Windows.Forms.MenuItem customConvolutionFiltersItem;
        private System.Windows.Forms.MenuItem openingMorphologyFiltersItem;
        private System.Windows.Forms.MenuItem medianFiltersItem;
        private System.Windows.Forms.MenuItem closingMorphologyFiltersItem;
        private System.Windows.Forms.MenuItem erosionMorphologyFiltersItem;
        private System.Windows.Forms.MenuItem pixellateFiltersItem;
        private System.Windows.Forms.MenuItem menuItem15;
        private System.Windows.Forms.MenuItem menuItem16;
        private System.Windows.Forms.MenuItem redColorFiltersItem;
        private System.Windows.Forms.MenuItem greenColorFiltersItem;
        private System.Windows.Forms.MenuItem blueColorFiltersItem;
        private System.Windows.Forms.MenuItem menuItem17;
        private System.Windows.Forms.MenuItem cyanColorFiltersItem;
        private System.Windows.Forms.MenuItem magentaColorFiltersItem;
        private System.Windows.Forms.MenuItem yellowColorFiltersItem;
        private System.Windows.Forms.MenuItem channelsFilteringColorFiltersItem;
        private System.Windows.Forms.MenuItem menuItem19;
        private System.Windows.Forms.MenuItem colorFilteringColorFiltersItem;
        private System.Windows.Forms.MenuItem euclideanFilteringColorFiltersItem;
        private System.Windows.Forms.MenuItem extractRedColorFiltersItem;
        private System.Windows.Forms.MenuItem extractGreenColorFiltersItem;
        private System.Windows.Forms.MenuItem extractRedBlueFiltersItem;
        private System.Windows.Forms.MenuItem menuItem18;
        private System.Windows.Forms.MenuItem replaceRedColorFiltersItem;
        private System.Windows.Forms.MenuItem menuItem20;
        private System.Windows.Forms.MenuItem replaceGreenColorFiltersItem;
        private System.Windows.Forms.MenuItem replaceBlueColorFiltersItem;
        private System.Windows.Forms.MenuItem twosrcFiltersItem;
        private System.Windows.Forms.MenuItem mergeTwosrcFiltersItem;
        private System.Windows.Forms.MenuItem intersectTwosrcFiltersItem;
        private System.Windows.Forms.MenuItem menuItem21;
        private System.Windows.Forms.MenuItem addTwosrcFiltersItem;
        private System.Windows.Forms.MenuItem subtractTwosrcFiltersItem;
        private System.Windows.Forms.MenuItem menuItem22;
        private System.Windows.Forms.MenuItem differenceTwosrcFiltersItem;
        private System.Windows.Forms.MenuItem moveTowardsTwosrcFiltersItem;
        private System.Windows.Forms.MenuItem simpleSkeletonizationFiltersItem;
        private System.Windows.Forms.MenuItem menuItem24;
        private System.Windows.Forms.MenuItem orderedDitherBinaryFiltersItem;
        private System.Windows.Forms.MenuItem menuItem14;
        private System.Windows.Forms.MenuItem bayerDitherBinaryFiltersItem;
        private System.Windows.Forms.MenuItem burkesBinaryFiltersItem;
        private System.Windows.Forms.MenuItem stuckiBinaryFiltersItem;
        private System.Windows.Forms.MenuItem jarvisBinaryFiltersItem;
        private System.Windows.Forms.MenuItem sierraBinaryFiltersItem;
        private System.Windows.Forms.MenuItem stevensonBinaryFiltersItem;
        private System.Windows.Forms.MenuItem menuItem23;
        private System.Windows.Forms.MenuItem resizeFiltersItem;
        private System.Windows.Forms.MenuItem menuItem26;
        private System.Windows.Forms.MenuItem shrinkFiltersItem;
        private System.Windows.Forms.MenuItem rotateFiltersItem;
        private System.Windows.Forms.MenuItem jitterFiltersItem;
        private System.Windows.Forms.MenuItem gammaFiltersItem;
        private System.Windows.Forms.MenuItem menuItem27;
        private System.Windows.Forms.MenuItem hitAndMissFiltersItem;
        private System.Windows.Forms.MenuItem hslFiltersItem;
        private System.Windows.Forms.MenuItem brightnessHslFiltersItem;
        private System.Windows.Forms.MenuItem contrastHslFiltersItem;
        private System.Windows.Forms.MenuItem saturationHslFiltersItem;
        private System.Windows.Forms.MenuItem menuItem28;
        private System.Windows.Forms.MenuItem filteringHslFiltersItem;
        private System.Windows.Forms.MenuItem hueHslFiltersItem;
        private System.Windows.Forms.MenuItem menuItem29;
        private System.Windows.Forms.MenuItem linearHslFiltersItem;
        private System.Windows.Forms.MenuItem menuItem25;
        private System.Windows.Forms.MenuItem fourierFiltersItem;
        private System.Windows.Forms.MenuItem edgeFiltersItem;
        private System.Windows.Forms.MenuItem homogenityEdgeFiltersItem;
        private System.Windows.Forms.MenuItem differenceEdgeFiltersItem;
        private System.Windows.Forms.MenuItem menuItem30;
        private System.Windows.Forms.MenuItem labelingFiltersItem;
        private System.Windows.Forms.MenuItem sobelEdgeFiltersItem;
        private System.Windows.Forms.MenuItem gaussianConvolutionFiltersItem;
        private System.Windows.Forms.MenuItem adaptiveSmoothingFiltersItem;
        private System.Windows.Forms.MenuItem blobExtractorFiltersItem;
        private System.Windows.Forms.MenuItem menuItem31;
        private System.Windows.Forms.MenuItem sisThresholdBinaryFiltersItem;
        private System.Windows.Forms.MenuItem conservativeSmoothingFiltersItem;
        private System.Windows.Forms.MenuItem menuItem34;
        private System.Windows.Forms.MenuItem cannyEdgeFiltersItem;
        private System.Windows.Forms.MenuItem menuItem33;
        private System.Windows.Forms.MenuItem sharpenExConvolutionFiltersItem;
        private System.Windows.Forms.MenuItem oilPaintingFiltersItem;
        private ContextMenu RightClickMenu;
        private MenuItem menuItem13;
        private MenuItem menuItem35;
        private MenuItem menuItem36;
        private MenuItem menuItem37;
        private MenuItem menuItem38;
        private MenuItem menuItem32;
        private MenuItem menuItem39;
        private MenuItem menuItem40;
        private MenuItem menuItem41;
        private MenuItem menuItem42;
        private MenuItem menuItem43;
        private ContextMenuStrip ContextMenuT;
        private ToolStripMenuItem deleteThisSliceToolStripMenuItem;
        private ToolStripSeparator toolStripSeparator4;
        private ToolStripMenuItem deleteSlicesBeforeToolStripMenuItem;
        private ToolStripMenuItem deleteSlicesAfterToolStripMenuItem;
        private ContextMenuStrip ContextMenuC;
        private ToolStripMenuItem toolStripMenuItem1;
        private ToolStripSeparator toolStripSeparator5;
        private ToolStripMenuItem toolStripMenuItem2;
        private ToolStripMenuItem toolStripMenuItem3;
        private ContextMenuStrip ContextMenuS;
        private ToolStripMenuItem toolStripMenuItem4;
        private ToolStripSeparator toolStripSeparator6;
        private ToolStripMenuItem toolStripMenuItem5;
        private ToolStripMenuItem toolStripMenuItem6;
        private MenuItem menuItem44;
        private MenuItem menuItem47;
        private MenuItem menuItem49;
        private MenuItem menuItem48;
        public MetaView.TrackBar sbar;
        public MetaView.TrackBar cbar;
        public MetaView.TrackBar tbar;
        private MenuItem menuItem50;
        private MenuItem menuItem51;
        private MenuItem menuItem52;
        private MenuItem menuItem53;
        private MenuItem menuItem54;
        private MenuItem menuItem55;
        private MenuItem menuItem56;
        private MenuItem menuItem57;
        private MenuItem menuItem58;
        private MenuItem menuItem59;
        private MenuItem menuItem60;
        private MenuItem autoFOV;
        private MenuItem menuItem61;
        private System.ComponentModel.IContainer components;

        // Image property
        public Bitmap Image
        {
            get { return image; }
        }
        // Width property
        public int ImageWidth
        {
            get { return width; }
        }
        // Height property
        public int ImageHeight
        {
            get { return height; }
        }
        // Zoom property
        public float Zoom
        {
            get { return zoom; }
        }
        // FileName property
        // return file name if the document was created from file or null
        public string FileName
        {
            get { return fileName; }
        }


        // Events
        public delegate void SelectionEventHandler(object sender, SelectionEventArgs e);

        public event EventHandler DocumentChanged;
        public event EventHandler ZoomChanged;
        public event SelectionEventHandler MouseImagePosition, MouseClickPosition;
        public event SelectionEventHandler SelectionChanged;
        public event EventHandler ScrollChanged;

        // Constructors
        private ImageDoc(IDocumentsHost host)
        {
            this.host = host;
        }
        // Construct from file
        public ImageDoc(string fileName, IDocumentsHost host)
            : this(host)
        {
            try
            {
                // load image
                image = (Bitmap)Bitmap.FromFile(fileName);

                // format image
                AForge.Imaging.Image.FormatImage(ref image);

                this.fileName = fileName;
            }
            catch (Exception)
            {
                throw new ApplicationException("Failed loading image");
            }
            Init();
            FitToScreen();
        }
        // Construct from image
        public ImageDoc(Bitmap image, IDocumentsHost host)
            : this(host)
        {
            this.image = image;
            AForge.Imaging.Image.FormatImage(ref this.image);

            Init();
            FitToScreen();
        }
        public event EventHandler slice;
        public event EventHandler BaseDatasetChanged;

public        MatrixDataStructures.Matrix3DInt16 Temp3DInt16;
      public  MatrixDataStructures.Matrix3DSingle Temp3DSingle;
        public MatrixDataStructures.ColorMatrix TempColorMatrix;
        CoordinateD fov, center;


        // Construct from image
        public ImageDoc(Int16[, ,] matrix, int x, int y, int z, IDocumentsHost host, CoordinateD fov, CoordinateD center)
            : this(host)
        {
            datamatrix = matrix;
            tempdatamatrix = matrix;
            this.x = x;
            this.y = y;
            this.z = z;
            this.image = CoronalSlice(this.y / 2);
            this.fov = fov;
            this.center = center;
            AForge.Imaging.Image.FormatImage(ref this.image);

            Init();
            cbar.Enabled = true;
            tbar.Enabled = true;
            sbar.Enabled = true;

            cbar.TickFrequency = Math.Max((int)(y / 15),1);
            tbar.TickFrequency = Math.Max((int)(z / 15),1);
            sbar.TickFrequency = Math.Max((int)(x / 15), 1);

            tbar.Maximum = z - 1;
            sbar.Maximum = x - 1;
            cbar.Maximum = y - 1;
            FitToScreen();

        }
        public ImageDoc(MatrixDataStructures.ColorMatrix matrix, IDocumentsHost host)
            : this(host)
        {
            this.CMatrix = matrix;
            this.x = matrix.x;
            this.y = matrix.y;
            this.z = matrix.z;
            this.currentType = "c";
            this.image = CMatrix.CoronalSlice(this.y / 2);
            AForge.Imaging.Image.FormatImage(ref this.image);

            Init();
            cbar.Enabled = true;
            tbar.Enabled = true;
            sbar.Enabled = true;

            cbar.TickFrequency = Math.Max((int)(y / 15), 1);
            tbar.TickFrequency = Math.Max((int)(z / 15), 1);
            sbar.TickFrequency = Math.Max((int)(x / 15), 1);

            tbar.Maximum = z - 1;
            sbar.Maximum = x - 1;
            cbar.Maximum = y - 1;
            FitToScreen();
            this.IsColorMatrixLoaded = true;

        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
                if (image != null)
                {
                    image.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.mainMenu = new System.Windows.Forms.MainMenu(this.components);
            this.imageItem = new System.Windows.Forms.MenuItem();
            this.backImageItem = new System.Windows.Forms.MenuItem();
            this.cloneImageItem = new System.Windows.Forms.MenuItem();
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.menuItem5 = new System.Windows.Forms.MenuItem();
            this.z10ImageItem = new System.Windows.Forms.MenuItem();
            this.z25ImageItem = new System.Windows.Forms.MenuItem();
            this.z50ImageItem = new System.Windows.Forms.MenuItem();
            this.z75ImageItem = new System.Windows.Forms.MenuItem();
            this.menuItem7 = new System.Windows.Forms.MenuItem();
            this.z100ImageItem = new System.Windows.Forms.MenuItem();
            this.menuItem6 = new System.Windows.Forms.MenuItem();
            this.z150ImageItem = new System.Windows.Forms.MenuItem();
            this.z200ImageItem = new System.Windows.Forms.MenuItem();
            this.z400ImageItem = new System.Windows.Forms.MenuItem();
            this.z500ImageItem = new System.Windows.Forms.MenuItem();
            this.menuItem8 = new System.Windows.Forms.MenuItem();
            this.zoomInImageItem = new System.Windows.Forms.MenuItem();
            this.zoomOutImageItem = new System.Windows.Forms.MenuItem();
            this.menuItem11 = new System.Windows.Forms.MenuItem();
            this.zoomFitImageItem = new System.Windows.Forms.MenuItem();
            this.menuItem10 = new System.Windows.Forms.MenuItem();
            this.flipImageItem = new System.Windows.Forms.MenuItem();
            this.mirrorItem = new System.Windows.Forms.MenuItem();
            this.rotateImageItem = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.cropImageItem = new System.Windows.Forms.MenuItem();
            this.filtersItem = new System.Windows.Forms.MenuItem();
            this.colorFiltersItem = new System.Windows.Forms.MenuItem();
            this.grayscaleColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.sepiaColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.invertColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.rotateColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem16 = new System.Windows.Forms.MenuItem();
            this.colorFilteringColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.euclideanFilteringColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.channelsFilteringColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem19 = new System.Windows.Forms.MenuItem();
            this.extractRedColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.extractGreenColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.extractRedBlueFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem20 = new System.Windows.Forms.MenuItem();
            this.replaceRedColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.replaceGreenColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.replaceBlueColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem18 = new System.Windows.Forms.MenuItem();
            this.redColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.greenColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.blueColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem17 = new System.Windows.Forms.MenuItem();
            this.cyanColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.magentaColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.yellowColorFiltersItem = new System.Windows.Forms.MenuItem();
            this.hslFiltersItem = new System.Windows.Forms.MenuItem();
            this.brightnessHslFiltersItem = new System.Windows.Forms.MenuItem();
            this.contrastHslFiltersItem = new System.Windows.Forms.MenuItem();
            this.saturationHslFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem29 = new System.Windows.Forms.MenuItem();
            this.linearHslFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem28 = new System.Windows.Forms.MenuItem();
            this.filteringHslFiltersItem = new System.Windows.Forms.MenuItem();
            this.hueHslFiltersItem = new System.Windows.Forms.MenuItem();
            this.binaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.thresholdBinaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem15 = new System.Windows.Forms.MenuItem();
            this.thresholdCarryBinaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.orderedDitherBinaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.bayerDitherBinaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem14 = new System.Windows.Forms.MenuItem();
            this.floydBinaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.burkesBinaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.stuckiBinaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.jarvisBinaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.sierraBinaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.stevensonBinaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem31 = new System.Windows.Forms.MenuItem();
            this.sisThresholdBinaryFiltersItem = new System.Windows.Forms.MenuItem();
            this.morphologyFiltersItem = new System.Windows.Forms.MenuItem();
            this.erosionMorphologyFiltersItem = new System.Windows.Forms.MenuItem();
            this.dilatationMorphologyFiltersItem = new System.Windows.Forms.MenuItem();
            this.openingMorphologyFiltersItem = new System.Windows.Forms.MenuItem();
            this.closingMorphologyFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem9 = new System.Windows.Forms.MenuItem();
            this.customMorphologyFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem27 = new System.Windows.Forms.MenuItem();
            this.hitAndMissFiltersItem = new System.Windows.Forms.MenuItem();
            this.convolutionFiltersItem = new System.Windows.Forms.MenuItem();
            this.meanConvolutionFiltersItem = new System.Windows.Forms.MenuItem();
            this.blurConvolutionFiltersItem = new System.Windows.Forms.MenuItem();
            this.sharpenConvolutionFiltersItem = new System.Windows.Forms.MenuItem();
            this.edgesConvolutionFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem12 = new System.Windows.Forms.MenuItem();
            this.customConvolutionFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem33 = new System.Windows.Forms.MenuItem();
            this.gaussianConvolutionFiltersItem = new System.Windows.Forms.MenuItem();
            this.sharpenExConvolutionFiltersItem = new System.Windows.Forms.MenuItem();
            this.twosrcFiltersItem = new System.Windows.Forms.MenuItem();
            this.mergeTwosrcFiltersItem = new System.Windows.Forms.MenuItem();
            this.intersectTwosrcFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem21 = new System.Windows.Forms.MenuItem();
            this.addTwosrcFiltersItem = new System.Windows.Forms.MenuItem();
            this.subtractTwosrcFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem22 = new System.Windows.Forms.MenuItem();
            this.differenceTwosrcFiltersItem = new System.Windows.Forms.MenuItem();
            this.moveTowardsTwosrcFiltersItem = new System.Windows.Forms.MenuItem();
            this.edgeFiltersItem = new System.Windows.Forms.MenuItem();
            this.homogenityEdgeFiltersItem = new System.Windows.Forms.MenuItem();
            this.differenceEdgeFiltersItem = new System.Windows.Forms.MenuItem();
            this.sobelEdgeFiltersItem = new System.Windows.Forms.MenuItem();
            this.cannyEdgeFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem24 = new System.Windows.Forms.MenuItem();
            this.adaptiveSmoothingFiltersItem = new System.Windows.Forms.MenuItem();
            this.conservativeSmoothingFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem34 = new System.Windows.Forms.MenuItem();
            this.oilPaintingFiltersItem = new System.Windows.Forms.MenuItem();
            this.jitterFiltersItem = new System.Windows.Forms.MenuItem();
            this.pixellateFiltersItem = new System.Windows.Forms.MenuItem();
            this.simpleSkeletonizationFiltersItem = new System.Windows.Forms.MenuItem();
            this.shrinkFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem30 = new System.Windows.Forms.MenuItem();
            this.labelingFiltersItem = new System.Windows.Forms.MenuItem();
            this.blobExtractorFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem23 = new System.Windows.Forms.MenuItem();
            this.resizeFiltersItem = new System.Windows.Forms.MenuItem();
            this.rotateFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem26 = new System.Windows.Forms.MenuItem();
            this.levelsFiltersItem = new System.Windows.Forms.MenuItem();
            this.medianFiltersItem = new System.Windows.Forms.MenuItem();
            this.gammaFiltersItem = new System.Windows.Forms.MenuItem();
            this.menuItem25 = new System.Windows.Forms.MenuItem();
            this.fourierFiltersItem = new System.Windows.Forms.MenuItem();
            this.ContextMenuC = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator();
            this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem();
            this.ContextMenuT = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.deleteThisSliceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            this.deleteSlicesBeforeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.deleteSlicesAfterToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.ContextMenuS = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator6 = new System.Windows.Forms.ToolStripSeparator();
            this.toolStripMenuItem5 = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripMenuItem6 = new System.Windows.Forms.ToolStripMenuItem();
            this.RightClickMenu = new System.Windows.Forms.ContextMenu();
            this.menuItem49 = new System.Windows.Forms.MenuItem();
            this.autoFOV = new System.Windows.Forms.MenuItem();
            this.menuItem61 = new System.Windows.Forms.MenuItem();
            this.menuItem54 = new System.Windows.Forms.MenuItem();
            this.menuItem55 = new System.Windows.Forms.MenuItem();
            this.menuItem56 = new System.Windows.Forms.MenuItem();
            this.menuItem57 = new System.Windows.Forms.MenuItem();
            this.menuItem58 = new System.Windows.Forms.MenuItem();
            this.menuItem59 = new System.Windows.Forms.MenuItem();
            this.menuItem48 = new System.Windows.Forms.MenuItem();
            this.menuItem35 = new System.Windows.Forms.MenuItem();
            this.menuItem13 = new System.Windows.Forms.MenuItem();
            this.menuItem36 = new System.Windows.Forms.MenuItem();
            this.menuItem37 = new System.Windows.Forms.MenuItem();
            this.menuItem38 = new System.Windows.Forms.MenuItem();
            this.menuItem32 = new System.Windows.Forms.MenuItem();
            this.menuItem39 = new System.Windows.Forms.MenuItem();
            this.menuItem40 = new System.Windows.Forms.MenuItem();
            this.menuItem41 = new System.Windows.Forms.MenuItem();
            this.menuItem42 = new System.Windows.Forms.MenuItem();
            this.menuItem43 = new System.Windows.Forms.MenuItem();
            this.menuItem44 = new System.Windows.Forms.MenuItem();
            this.menuItem60 = new System.Windows.Forms.MenuItem();
            this.menuItem47 = new System.Windows.Forms.MenuItem();
            this.menuItem50 = new System.Windows.Forms.MenuItem();
            this.menuItem51 = new System.Windows.Forms.MenuItem();
            this.menuItem52 = new System.Windows.Forms.MenuItem();
            this.menuItem53 = new System.Windows.Forms.MenuItem();
            this.sbar = new MetaView.TrackBar();
            this.cbar = new MetaView.TrackBar();
            this.tbar = new MetaView.TrackBar();
            this.ContextMenuC.SuspendLayout();
            this.ContextMenuT.SuspendLayout();
            this.ContextMenuS.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.sbar)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.cbar)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.tbar)).BeginInit();
            this.SuspendLayout();
            // 
            // imageItem
            // 
            this.imageItem.Index = 5;
            this.imageItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.backImageItem,
            this.cloneImageItem,
            this.menuItem4,
            this.menuItem5,
            this.menuItem10,
            this.flipImageItem,
            this.mirrorItem,
            this.rotateImageItem,
            this.menuItem3,
            this.cropImageItem});
            this.imageItem.MergeOrder = 1;
            this.imageItem.Text = "&Image";
            this.imageItem.Popup += new System.EventHandler(this.imageItem_Popup);
            // 
            // backImageItem
            // 
            this.backImageItem.Index = 0;
            this.backImageItem.Shortcut = System.Windows.Forms.Shortcut.CtrlZ;
            this.backImageItem.Text = "&Back";
            this.backImageItem.Click += new System.EventHandler(this.backImageItem_Click);
            // 
            // cloneImageItem
            // 
            this.cloneImageItem.Index = 1;
            this.cloneImageItem.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
            this.cloneImageItem.Text = "&Clone";
            this.cloneImageItem.Click += new System.EventHandler(this.cloneImageItem_Click);
            // 
            // menuItem4
            // 
            this.menuItem4.Index = 2;
            this.menuItem4.Text = "-";
            // 
            // menuItem5
            // 
            this.menuItem5.Index = 3;
            this.menuItem5.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.z10ImageItem,
            this.z25ImageItem,
            this.z50ImageItem,
            this.z75ImageItem,
            this.menuItem7,
            this.z100ImageItem,
            this.menuItem6,
            this.z150ImageItem,
            this.z200ImageItem,
            this.z400ImageItem,
            this.z500ImageItem,
            this.menuItem8,
            this.zoomInImageItem,
            this.zoomOutImageItem,
            this.menuItem11,
            this.zoomFitImageItem});
            this.menuItem5.Text = "&Zoom";
            // 
            // z10ImageItem
            // 
            this.z10ImageItem.Index = 0;
            this.z10ImageItem.Text = "10%";
            this.z10ImageItem.Click += new System.EventHandler(this.zoomItem_Click);
            // 
            // z25ImageItem
            // 
            this.z25ImageItem.Index = 1;
            this.z25ImageItem.Text = "25%";
            this.z25ImageItem.Click += new System.EventHandler(this.zoomItem_Click);
            // 
            // z50ImageItem
            // 
            this.z50ImageItem.Index = 2;
            this.z50ImageItem.Text = "50%";
            this.z50ImageItem.Click += new System.EventHandler(this.zoomItem_Click);
            // 
            // z75ImageItem
            // 
            this.z75ImageItem.Index = 3;
            this.z75ImageItem.Text = "75%";
            this.z75ImageItem.Click += new System.EventHandler(this.zoomItem_Click);
            // 
            // menuItem7
            // 
            this.menuItem7.Index = 4;
            this.menuItem7.Text = "-";
            // 
            // z100ImageItem
            // 
            this.z100ImageItem.Index = 5;
            this.z100ImageItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl0;
            this.z100ImageItem.Text = "100%";
            this.z100ImageItem.Click += new System.EventHandler(this.zoomItem_Click);
            // 
            // menuItem6
            // 
            this.menuItem6.Index = 6;
            this.menuItem6.Text = "-";
            // 
            // z150ImageItem
            // 
            this.z150ImageItem.Index = 7;
            this.z150ImageItem.Text = "150%";
            this.z150ImageItem.Click += new System.EventHandler(this.zoomItem_Click);
            // 
            // z200ImageItem
            // 
            this.z200ImageItem.Index = 8;
            this.z200ImageItem.Text = "200%";
            this.z200ImageItem.Click += new System.EventHandler(this.zoomItem_Click);
            // 
            // z400ImageItem
            // 
            this.z400ImageItem.Index = 9;
            this.z400ImageItem.Text = "400%";
            this.z400ImageItem.Click += new System.EventHandler(this.zoomItem_Click);
            // 
            // z500ImageItem
            // 
            this.z500ImageItem.Index = 10;
            this.z500ImageItem.Text = "500%";
            this.z500ImageItem.Click += new System.EventHandler(this.zoomItem_Click);
            // 
            // menuItem8
            // 
            this.menuItem8.Index = 11;
            this.menuItem8.Text = "-";
            // 
            // zoomInImageItem
            // 
            this.zoomInImageItem.Index = 12;
            this.zoomInImageItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl8;
            this.zoomInImageItem.Text = "Zoom &In";
            this.zoomInImageItem.Click += new System.EventHandler(this.zoomInImageItem_Click);
            // 
            // zoomOutImageItem
            // 
            this.zoomOutImageItem.Index = 13;
            this.zoomOutImageItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl7;
            this.zoomOutImageItem.Text = "Zoom &Out";
            this.zoomOutImageItem.Click += new System.EventHandler(this.zoomOutImageItem_Click);
            // 
            // menuItem11
            // 
            this.menuItem11.Index = 14;
            this.menuItem11.Text = "-";
            // 
            // zoomFitImageItem
            // 
            this.zoomFitImageItem.Index = 15;
            this.zoomFitImageItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl9;
            this.zoomFitImageItem.Text = "Fit to screen";
            this.zoomFitImageItem.Click += new System.EventHandler(this.zoomFitImageItem_Click);
            // 
            // menuItem10
            // 
            this.menuItem10.Index = 4;
            this.menuItem10.Text = "-";
            // 
            // flipImageItem
            // 
            this.flipImageItem.Index = 5;
            this.flipImageItem.Text = "&Flip";
            this.flipImageItem.Click += new System.EventHandler(this.flipImageItem_Click);
            // 
            // mirrorItem
            // 
            this.mirrorItem.Index = 6;
            this.mirrorItem.Text = "&Mirror";
            this.mirrorItem.Click += new System.EventHandler(this.mirrorItem_Click);
            // 
            // rotateImageItem
            // 
            this.rotateImageItem.Index = 7;
            this.rotateImageItem.Text = "&Rotate 90 degree";
            this.rotateImageItem.Click += new System.EventHandler(this.rotateImageItem_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 8;
            this.menuItem3.Text = "-";
            // 
            // cropImageItem
            // 
            this.cropImageItem.Index = 9;
            this.cropImageItem.Shortcut = System.Windows.Forms.Shortcut.CtrlE;
            this.cropImageItem.Text = "Cro&p";
            this.cropImageItem.Click += new System.EventHandler(this.cropImageItem_Click);
            // 
            // filtersItem
            // 
            this.filtersItem.Index = 6;
            this.filtersItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.colorFiltersItem,
            this.hslFiltersItem,
            this.binaryFiltersItem,
            this.morphologyFiltersItem,
            this.convolutionFiltersItem,
            this.twosrcFiltersItem,
            this.edgeFiltersItem,
            this.menuItem24,
            this.menuItem23,
            this.resizeFiltersItem,
            this.rotateFiltersItem,
            this.menuItem26,
            this.levelsFiltersItem,
            this.medianFiltersItem,
            this.gammaFiltersItem,
            this.menuItem25,
            this.fourierFiltersItem});
            this.filtersItem.MergeOrder = 1;
            this.filtersItem.Text = "Fi&lters";
            // 
            // colorFiltersItem
            // 
            this.colorFiltersItem.Index = 0;
            this.colorFiltersItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.grayscaleColorFiltersItem,
            this.menuItem1,
            this.sepiaColorFiltersItem,
            this.menuItem2,
            this.invertColorFiltersItem,
            this.rotateColorFiltersItem,
            this.menuItem16,
            this.colorFilteringColorFiltersItem,
            this.euclideanFilteringColorFiltersItem,
            this.channelsFilteringColorFiltersItem,
            this.menuItem19,
            this.extractRedColorFiltersItem,
            this.extractGreenColorFiltersItem,
            this.extractRedBlueFiltersItem,
            this.menuItem20,
            this.replaceRedColorFiltersItem,
            this.replaceGreenColorFiltersItem,
            this.replaceBlueColorFiltersItem,
            this.menuItem18,
            this.redColorFiltersItem,
            this.greenColorFiltersItem,
            this.blueColorFiltersItem,
            this.menuItem17,
            this.cyanColorFiltersItem,
            this.magentaColorFiltersItem,
            this.yellowColorFiltersItem});
            this.colorFiltersItem.Text = "&Color";
            // 
            // grayscaleColorFiltersItem
            // 
            this.grayscaleColorFiltersItem.Index = 0;
            this.grayscaleColorFiltersItem.Text = "&Grayscale";
            this.grayscaleColorFiltersItem.Click += new System.EventHandler(this.grayscaleColorFiltersItem_Click);
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 1;
            this.menuItem1.Text = "-";
            // 
            // sepiaColorFiltersItem
            // 
            this.sepiaColorFiltersItem.Index = 2;
            this.sepiaColorFiltersItem.Text = "&Sepia";
            this.sepiaColorFiltersItem.Click += new System.EventHandler(this.sepiaColorFiltersItem_Click);
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 3;
            this.menuItem2.Text = "-";
            // 
            // invertColorFiltersItem
            // 
            this.invertColorFiltersItem.Index = 4;
            this.invertColorFiltersItem.Text = "&Invert";
            this.invertColorFiltersItem.Click += new System.EventHandler(this.invertColorFiltersItem_Click);
            // 
            // rotateColorFiltersItem
            // 
            this.rotateColorFiltersItem.Index = 5;
            this.rotateColorFiltersItem.Text = "&Rotate";
            this.rotateColorFiltersItem.Click += new System.EventHandler(this.rotateColorFiltersItem_Click);
            // 
            // menuItem16
            // 
            this.menuItem16.Index = 6;
            this.menuItem16.Text = "-";
            // 
            // colorFilteringColorFiltersItem
            // 
            this.colorFilteringColorFiltersItem.DefaultItem = true;
            this.colorFilteringColorFiltersItem.Index = 7;
            this.colorFilteringColorFiltersItem.Text = "Thresholding / Color Filtering";
            this.colorFilteringColorFiltersItem.Click += new System.EventHandler(this.colorFilteringColorFiltersItem_Click);
            // 
            // euclideanFilteringColorFiltersItem
            // 
            this.euclideanFilteringColorFiltersItem.Index = 8;
            this.euclideanFilteringColorFiltersItem.Text = "Euclidean Color Filtering";
            this.euclideanFilteringColorFiltersItem.Click += new System.EventHandler(this.euclideanFilteringColorFiltersItem_Click);
            // 
            // channelsFilteringColorFiltersItem
            // 
            this.channelsFilteringColorFiltersItem.Index = 9;
            this.channelsFilteringColorFiltersItem.Text = "Channels Filtering";
            this.channelsFilteringColorFiltersItem.Click += new System.EventHandler(this.channelsFilteringColorFiltersItem_Click);
            // 
            // menuItem19
            // 
            this.menuItem19.Index = 10;
            this.menuItem19.Text = "-";
            // 
            // extractRedColorFiltersItem
            // 
            this.extractRedColorFiltersItem.Index = 11;
            this.extractRedColorFiltersItem.Text = "Extract Red Channel";
            this.extractRedColorFiltersItem.Click += new System.EventHandler(this.extractRedColorFiltersItem_Click);
            // 
            // extractGreenColorFiltersItem
            // 
            this.extractGreenColorFiltersItem.Index = 12;
            this.extractGreenColorFiltersItem.Text = "Extract Green Channel";
            this.extractGreenColorFiltersItem.Click += new System.EventHandler(this.extractGreenColorFiltersItem_Click);
            // 
            // extractRedBlueFiltersItem
            // 
            this.extractRedBlueFiltersItem.Index = 13;
            this.extractRedBlueFiltersItem.Text = "Extract Blue Channel";
            this.extractRedBlueFiltersItem.Click += new System.EventHandler(this.extractRedBlueFiltersItem_Click);
            // 
            // menuItem20
            // 
            this.menuItem20.Index = 14;
            this.menuItem20.Text = "-";
            // 
            // replaceRedColorFiltersItem
            // 
            this.replaceRedColorFiltersItem.Index = 15;
            this.replaceRedColorFiltersItem.Text = "Replace Red Channel";
            this.replaceRedColorFiltersItem.Click += new System.EventHandler(this.replaceRedColorFiltersItem_Click);
            // 
            // replaceGreenColorFiltersItem
            // 
            this.replaceGreenColorFiltersItem.Index = 16;
            this.replaceGreenColorFiltersItem.Text = "Replace Green Channel";
            this.replaceGreenColorFiltersItem.Click += new System.EventHandler(this.replaceGreenColorFiltersItem_Click);
            // 
            // replaceBlueColorFiltersItem
            // 
            this.replaceBlueColorFiltersItem.Index = 17;
            this.replaceBlueColorFiltersItem.Text = "Replace Blue Channel";
            this.replaceBlueColorFiltersItem.Click += new System.EventHandler(this.replaceBlueColorFiltersItem_Click);
            // 
            // menuItem18
            // 
            this.menuItem18.Index = 18;
            this.menuItem18.Text = "-";
            // 
            // redColorFiltersItem
            // 
            this.redColorFiltersItem.Index = 19;
            this.redColorFiltersItem.Text = "Red";
            this.redColorFiltersItem.Click += new System.EventHandler(this.redColorFiltersItem_Click);
            // 
            // greenColorFiltersItem
            // 
            this.greenColorFiltersItem.Index = 20;
            this.greenColorFiltersItem.Text = "Green";
            this.greenColorFiltersItem.Click += new System.EventHandler(this.greenColorFiltersItem_Click);
            // 
            // blueColorFiltersItem
            // 
            this.blueColorFiltersItem.Index = 21;
            this.blueColorFiltersItem.Text = "Blue";
            this.blueColorFiltersItem.Click += new System.EventHandler(this.blueColorFiltersItem_Click);
            // 
            // menuItem17
            // 
            this.menuItem17.Index = 22;
            this.menuItem17.Text = "-";
            // 
            // cyanColorFiltersItem
            // 
            this.cyanColorFiltersItem.Index = 23;
            this.cyanColorFiltersItem.Text = "Cyan";
            this.cyanColorFiltersItem.Click += new System.EventHandler(this.cyanColorFiltersItem_Click);
            // 
            // magentaColorFiltersItem
            // 
            this.magentaColorFiltersItem.Index = 24;
            this.magentaColorFiltersItem.Text = "Magenta";
            this.magentaColorFiltersItem.Click += new System.EventHandler(this.magentaColorFiltersItem_Click);
            // 
            // yellowColorFiltersItem
            // 
            this.yellowColorFiltersItem.Index = 25;
            this.yellowColorFiltersItem.Text = "Yellow";
            this.yellowColorFiltersItem.Click += new System.EventHandler(this.yellowColorFiltersItem_Click);
            // 
            // hslFiltersItem
            // 
            this.hslFiltersItem.Index = 1;
            this.hslFiltersItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.brightnessHslFiltersItem,
            this.contrastHslFiltersItem,
            this.saturationHslFiltersItem,
            this.menuItem29,
            this.linearHslFiltersItem,
            this.menuItem28,
            this.filteringHslFiltersItem,
            this.hueHslFiltersItem});
            this.hslFiltersItem.Text = "&HSL Color space";
            // 
            // brightnessHslFiltersItem
            // 
            this.brightnessHslFiltersItem.Index = 0;
            this.brightnessHslFiltersItem.Text = "&Brightness";
            this.brightnessHslFiltersItem.Click += new System.EventHandler(this.brightnessHslFiltersItem_Click);
            // 
            // contrastHslFiltersItem
            // 
            this.contrastHslFiltersItem.Index = 1;
            this.contrastHslFiltersItem.Text = "&Contrast";
            this.contrastHslFiltersItem.Click += new System.EventHandler(this.contrastHslFiltersItem_Click);
            // 
            // saturationHslFiltersItem
            // 
            this.saturationHslFiltersItem.Index = 2;
            this.saturationHslFiltersItem.Text = "&Saturation";
            this.saturationHslFiltersItem.Click += new System.EventHandler(this.saturationHslFiltersItem_Click);
            // 
            // menuItem29
            // 
            this.menuItem29.Index = 3;
            this.menuItem29.Text = "-";
            // 
            // linearHslFiltersItem
            // 
            this.linearHslFiltersItem.Index = 4;
            this.linearHslFiltersItem.Text = "HSL Linear";
            this.linearHslFiltersItem.Click += new System.EventHandler(this.linearHslFiltersItem_Click);
            // 
            // menuItem28
            // 
            this.menuItem28.Index = 5;
            this.menuItem28.Text = "-";
            // 
            // filteringHslFiltersItem
            // 
            this.filteringHslFiltersItem.Index = 6;
            this.filteringHslFiltersItem.Text = "HSL &Filtering";
            this.filteringHslFiltersItem.Click += new System.EventHandler(this.filteringHslFiltersItem_Click);
            // 
            // hueHslFiltersItem
            // 
            this.hueHslFiltersItem.Index = 7;
            this.hueHslFiltersItem.Text = "&Hue Modifier";
            this.hueHslFiltersItem.Click += new System.EventHandler(this.hueHslFiltersItem_Click);
            // 
            // binaryFiltersItem
            // 
            this.binaryFiltersItem.Index = 2;
            this.binaryFiltersItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.thresholdBinaryFiltersItem,
            this.menuItem15,
            this.thresholdCarryBinaryFiltersItem,
            this.orderedDitherBinaryFiltersItem,
            this.bayerDitherBinaryFiltersItem,
            this.menuItem14,
            this.floydBinaryFiltersItem,
            this.burkesBinaryFiltersItem,
            this.stuckiBinaryFiltersItem,
            this.jarvisBinaryFiltersItem,
            this.sierraBinaryFiltersItem,
            this.stevensonBinaryFiltersItem,
            this.menuItem31,
            this.sisThresholdBinaryFiltersItem});
            this.binaryFiltersItem.Text = "&Binarization";
            // 
            // thresholdBinaryFiltersItem
            // 
            this.thresholdBinaryFiltersItem.Index = 0;
            this.thresholdBinaryFiltersItem.Text = "&Threshold";
            this.thresholdBinaryFiltersItem.Click += new System.EventHandler(this.thresholdBinaryFiltersItem_Click);
            // 
            // menuItem15
            // 
            this.menuItem15.Index = 1;
            this.menuItem15.Text = "-";
            // 
            // thresholdCarryBinaryFiltersItem
            // 
            this.thresholdCarryBinaryFiltersItem.Index = 2;
            this.thresholdCarryBinaryFiltersItem.Text = "Threshold with Error &Carry";
            this.thresholdCarryBinaryFiltersItem.Click += new System.EventHandler(this.thresholdCarryBinaryFiltersItem_Click);
            // 
            // orderedDitherBinaryFiltersItem
            // 
            this.orderedDitherBinaryFiltersItem.Index = 3;
            this.orderedDitherBinaryFiltersItem.Text = "&Ordered Dither";
            this.orderedDitherBinaryFiltersItem.Click += new System.EventHandler(this.orderedDitherBinaryFiltersItem_Click);
            // 
            // bayerDitherBinaryFiltersItem
            // 
            this.bayerDitherBinaryFiltersItem.Index = 4;
            this.bayerDitherBinaryFiltersItem.Text = "Ba&yer Ordered Dither";
            this.bayerDitherBinaryFiltersItem.Click += new System.EventHandler(this.bayerDitherBinaryFiltersItem_Click);
            // 
            // menuItem14
            // 
            this.menuItem14.Index = 5;
            this.menuItem14.Text = "-";
            // 
            // floydBinaryFiltersItem
            // 
            this.floydBinaryFiltersItem.Index = 6;
            this.floydBinaryFiltersItem.Text = "&Floyd-Steinberg";
            this.floydBinaryFiltersItem.Click += new System.EventHandler(this.floydBinaryFiltersItem_Click);
            // 
            // burkesBinaryFiltersItem
            // 
            this.burkesBinaryFiltersItem.Index = 7;
            this.burkesBinaryFiltersItem.Text = "&Burkes";
            this.burkesBinaryFiltersItem.Click += new System.EventHandler(this.burkesBinaryFiltersItem_Click);
            // 
            // stuckiBinaryFiltersItem
            // 
            this.stuckiBinaryFiltersItem.Index = 8;
            this.stuckiBinaryFiltersItem.Text = "&Stucki";
            this.stuckiBinaryFiltersItem.Click += new System.EventHandler(this.stuckiBinaryFiltersItem_Click);
            // 
            // jarvisBinaryFiltersItem
            // 
            this.jarvisBinaryFiltersItem.Index = 9;
            this.jarvisBinaryFiltersItem.Text = "&Jarvis-Judice-Ninke";
            this.jarvisBinaryFiltersItem.Click += new System.EventHandler(this.jarvisBinaryFiltersItem_Click);
            // 
            // sierraBinaryFiltersItem
            // 
            this.sierraBinaryFiltersItem.Index = 10;
            this.sierraBinaryFiltersItem.Text = "Sie&rra";
            this.sierraBinaryFiltersItem.Click += new System.EventHandler(this.sierraBinaryFiltersItem_Click);
            // 
            // stevensonBinaryFiltersItem
            // 
            this.stevensonBinaryFiltersItem.Index = 11;
            this.stevensonBinaryFiltersItem.Text = "Stevenson and &Arce";
            this.stevensonBinaryFiltersItem.Click += new System.EventHandler(this.stevensonBinaryFiltersItem_Click);
            // 
            // menuItem31
            // 
            this.menuItem31.Index = 12;
            this.menuItem31.Text = "-";
            // 
            // sisThresholdBinaryFiltersItem
            // 
            this.sisThresholdBinaryFiltersItem.Index = 13;
            this.sisThresholdBinaryFiltersItem.Text = "SIS Threshold";
            this.sisThresholdBinaryFiltersItem.Click += new System.EventHandler(this.sisThresholdBinaryFiltersItem_Click);
            // 
            // morphologyFiltersItem
            // 
            this.morphologyFiltersItem.Index = 3;
            this.morphologyFiltersItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.erosionMorphologyFiltersItem,
            this.dilatationMorphologyFiltersItem,
            this.openingMorphologyFiltersItem,
            this.closingMorphologyFiltersItem,
            this.menuItem9,
            this.customMorphologyFiltersItem,
            this.menuItem27,
            this.hitAndMissFiltersItem});
            this.morphologyFiltersItem.Text = "&Morphology";
            // 
            // erosionMorphologyFiltersItem
            // 
            this.erosionMorphologyFiltersItem.Index = 0;
            this.erosionMorphologyFiltersItem.Text = "&Erosion";
            this.erosionMorphologyFiltersItem.Click += new System.EventHandler(this.erosionMorphologyFiltersItem_Click);
            // 
            // dilatationMorphologyFiltersItem
            // 
            this.dilatationMorphologyFiltersItem.Index = 1;
            this.dilatationMorphologyFiltersItem.Text = "&Dilatation";
            this.dilatationMorphologyFiltersItem.Click += new System.EventHandler(this.dilatationMorphologyFiltersItem_Click);
            // 
            // openingMorphologyFiltersItem
            // 
            this.openingMorphologyFiltersItem.Index = 2;
            this.openingMorphologyFiltersItem.Text = "&Opening";
            this.openingMorphologyFiltersItem.Click += new System.EventHandler(this.openingMorphologyFiltersItem_Click);
            // 
            // closingMorphologyFiltersItem
            // 
            this.closingMorphologyFiltersItem.Index = 3;
            this.closingMorphologyFiltersItem.Text = "&Closing";
            this.closingMorphologyFiltersItem.Click += new System.EventHandler(this.closingMorphologyFiltersItem_Click);
            // 
            // menuItem9
            // 
            this.menuItem9.Index = 4;
            this.menuItem9.Text = "-";
            // 
            // customMorphologyFiltersItem
            // 
            this.customMorphologyFiltersItem.Index = 5;
            this.customMorphologyFiltersItem.Text = "Cus&tom";
            this.customMorphologyFiltersItem.Click += new System.EventHandler(this.customMorphologyFiltersItem_Click);
            // 
            // menuItem27
            // 
            this.menuItem27.Index = 6;
            this.menuItem27.Text = "-";
            // 
            // hitAndMissFiltersItem
            // 
            this.hitAndMissFiltersItem.Index = 7;
            this.hitAndMissFiltersItem.Text = "Hit And Miss, Thickening, Thinning";
            this.hitAndMissFiltersItem.Click += new System.EventHandler(this.hitAndMissFiltersItem_Click);
            // 
            // convolutionFiltersItem
            // 
            this.convolutionFiltersItem.Index = 4;
            this.convolutionFiltersItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.meanConvolutionFiltersItem,
            this.blurConvolutionFiltersItem,
            this.sharpenConvolutionFiltersItem,
            this.edgesConvolutionFiltersItem,
            this.menuItem12,
            this.customConvolutionFiltersItem,
            this.menuItem33,
            this.gaussianConvolutionFiltersItem,
            this.sharpenExConvolutionFiltersItem});
            this.convolutionFiltersItem.Text = "Co&nvolution && Correlation";
            // 
            // meanConvolutionFiltersItem
            // 
            this.meanConvolutionFiltersItem.Index = 0;
            this.meanConvolutionFiltersItem.Text = "&Mean";
            this.meanConvolutionFiltersItem.Click += new System.EventHandler(this.meanConvolutionFiltersItem_Click);
            // 
            // blurConvolutionFiltersItem
            // 
            this.blurConvolutionFiltersItem.Index = 1;
            this.blurConvolutionFiltersItem.Text = "&Blur";
            this.blurConvolutionFiltersItem.Click += new System.EventHandler(this.blurConvolutionFiltersItem_Click);
            // 
            // sharpenConvolutionFiltersItem
            // 
            this.sharpenConvolutionFiltersItem.Index = 2;
            this.sharpenConvolutionFiltersItem.Text = "&Sharpen";
            this.sharpenConvolutionFiltersItem.Click += new System.EventHandler(this.sharpenConvolutionFiltersItem_Click);
            // 
            // edgesConvolutionFiltersItem
            // 
            this.edgesConvolutionFiltersItem.Index = 3;
            this.edgesConvolutionFiltersItem.Text = "&Edges";
            this.edgesConvolutionFiltersItem.Click += new System.EventHandler(this.edgesConvolutionFiltersItem_Click);
            // 
            // menuItem12
            // 
            this.menuItem12.Index = 4;
            this.menuItem12.Text = "-";
            // 
            // customConvolutionFiltersItem
            // 
            this.customConvolutionFiltersItem.Index = 5;
            this.customConvolutionFiltersItem.Text = "&Custom";
            this.customConvolutionFiltersItem.Click += new System.EventHandler(this.customConvolutionFiltersItem_Click);
            // 
            // menuItem33
            // 
            this.menuItem33.Index = 6;
            this.menuItem33.Text = "-";
            // 
            // gaussianConvolutionFiltersItem
            // 
            this.gaussianConvolutionFiltersItem.Index = 7;
            this.gaussianConvolutionFiltersItem.Text = "&Gaussian";
            this.gaussianConvolutionFiltersItem.Click += new System.EventHandler(this.gaussianConvolutionFiltersItem_Click);
            // 
            // sharpenExConvolutionFiltersItem
            // 
            this.sharpenExConvolutionFiltersItem.Index = 8;
            this.sharpenExConvolutionFiltersItem.Text = "Sharpen Ex";
            this.sharpenExConvolutionFiltersItem.Click += new System.EventHandler(this.sharpenExConvolutionFiltersItem_Click);
            // 
            // twosrcFiltersItem
            // 
            this.twosrcFiltersItem.Index = 5;
            this.twosrcFiltersItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mergeTwosrcFiltersItem,
            this.intersectTwosrcFiltersItem,
            this.menuItem21,
            this.addTwosrcFiltersItem,
            this.subtractTwosrcFiltersItem,
            this.menuItem22,
            this.differenceTwosrcFiltersItem,
            this.moveTowardsTwosrcFiltersItem});
            this.twosrcFiltersItem.Text = "Two source filters";
            // 
            // mergeTwosrcFiltersItem
            // 
            this.mergeTwosrcFiltersItem.Index = 0;
            this.mergeTwosrcFiltersItem.Text = "&Merge";
            this.mergeTwosrcFiltersItem.Click += new System.EventHandler(this.mergeTwosrcFiltersItem_Click);
            // 
            // intersectTwosrcFiltersItem
            // 
            this.intersectTwosrcFiltersItem.Index = 1;
            this.intersectTwosrcFiltersItem.Text = "&Intersect";
            this.intersectTwosrcFiltersItem.Click += new System.EventHandler(this.intersectTwosrcFiltersItem_Click);
            // 
            // menuItem21
            // 
            this.menuItem21.Index = 2;
            this.menuItem21.Text = "-";
            // 
            // addTwosrcFiltersItem
            // 
            this.addTwosrcFiltersItem.Index = 3;
            this.addTwosrcFiltersItem.Text = "&Add";
            this.addTwosrcFiltersItem.Click += new System.EventHandler(this.addTwosrcFiltersItem_Click);
            // 
            // subtractTwosrcFiltersItem
            // 
            this.subtractTwosrcFiltersItem.Index = 4;
            this.subtractTwosrcFiltersItem.Text = "&Subtract";
            this.subtractTwosrcFiltersItem.Click += new System.EventHandler(this.subtractTwosrcFiltersItem_Click);
            // 
            // menuItem22
            // 
            this.menuItem22.Index = 5;
            this.menuItem22.Text = "-";
            // 
            // differenceTwosrcFiltersItem
            // 
            this.differenceTwosrcFiltersItem.Index = 6;
            this.differenceTwosrcFiltersItem.Text = "&Difference";
            this.differenceTwosrcFiltersItem.Click += new System.EventHandler(this.differenceTwosrcFiltersItem_Click);
            // 
            // moveTowardsTwosrcFiltersItem
            // 
            this.moveTowardsTwosrcFiltersItem.Index = 7;
            this.moveTowardsTwosrcFiltersItem.Text = "&Move Towards";
            this.moveTowardsTwosrcFiltersItem.Click += new System.EventHandler(this.moveTowardsTwosrcFiltersItem_Click);
            // 
            // edgeFiltersItem
            // 
            this.edgeFiltersItem.Index = 6;
            this.edgeFiltersItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.homogenityEdgeFiltersItem,
            this.differenceEdgeFiltersItem,
            this.sobelEdgeFiltersItem,
            this.cannyEdgeFiltersItem});
            this.edgeFiltersItem.Text = "&Edge detectors";
            // 
            // homogenityEdgeFiltersItem
            // 
            this.homogenityEdgeFiltersItem.Index = 0;
            this.homogenityEdgeFiltersItem.Text = "&Homogenity";
            this.homogenityEdgeFiltersItem.Click += new System.EventHandler(this.homogenityEdgeFiltersItem_Click);
            // 
            // differenceEdgeFiltersItem
            // 
            this.differenceEdgeFiltersItem.Index = 1;
            this.differenceEdgeFiltersItem.Text = "&Difference";
            this.differenceEdgeFiltersItem.Click += new System.EventHandler(this.differenceEdgeFiltersItem_Click);
            // 
            // sobelEdgeFiltersItem
            // 
            this.sobelEdgeFiltersItem.Index = 2;
            this.sobelEdgeFiltersItem.Text = "&Sobel";
            this.sobelEdgeFiltersItem.Click += new System.EventHandler(this.sobelEdgeFiltersItem_Click);
            // 
            // cannyEdgeFiltersItem
            // 
            this.cannyEdgeFiltersItem.Index = 3;
            this.cannyEdgeFiltersItem.Text = "&Canny";
            this.cannyEdgeFiltersItem.Click += new System.EventHandler(this.cannyEdgeFiltersItem_Click);
            // 
            // menuItem24
            // 
            this.menuItem24.Index = 7;
            this.menuItem24.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.adaptiveSmoothingFiltersItem,
            this.conservativeSmoothingFiltersItem,
            this.menuItem34,
            this.oilPaintingFiltersItem,
            this.jitterFiltersItem,
            this.pixellateFiltersItem,
            this.simpleSkeletonizationFiltersItem,
            this.shrinkFiltersItem,
            this.menuItem30,
            this.labelingFiltersItem,
            this.blobExtractorFiltersItem});
            this.menuItem24.Text = "Other";
            // 
            // adaptiveSmoothingFiltersItem
            // 
            this.adaptiveSmoothingFiltersItem.Index = 0;
            this.adaptiveSmoothingFiltersItem.Text = "&Adaptive Smoothing";
            this.adaptiveSmoothingFiltersItem.Click += new System.EventHandler(this.adaptiveSmoothingFiltersItem_Click);
            // 
            // conservativeSmoothingFiltersItem
            // 
            this.conservativeSmoothingFiltersItem.Index = 1;
            this.conservativeSmoothingFiltersItem.Text = "&ConservativeSmoothing";
            this.conservativeSmoothingFiltersItem.Click += new System.EventHandler(this.conservativeSmoothingFiltersItem_Click);
            // 
            // menuItem34
            // 
            this.menuItem34.Index = 2;
            this.menuItem34.Text = "-";
            // 
            // oilPaintingFiltersItem
            // 
            this.oilPaintingFiltersItem.Index = 3;
            this.oilPaintingFiltersItem.Text = "&Oil Painting";
            this.oilPaintingFiltersItem.Click += new System.EventHandler(this.oilPaintingFiltersItem_Click);
            // 
            // jitterFiltersItem
            // 
            this.jitterFiltersItem.Index = 4;
            this.jitterFiltersItem.Text = "&Jitter";
            this.jitterFiltersItem.Click += new System.EventHandler(this.jitterFiltersItem_Click);
            // 
            // pixellateFiltersItem
            // 
            this.pixellateFiltersItem.Index = 5;
            this.pixellateFiltersItem.Text = "&Pixellate";
            this.pixellateFiltersItem.Click += new System.EventHandler(this.pixellateFiltersItem_Click);
            // 
            // simpleSkeletonizationFiltersItem
            // 
            this.simpleSkeletonizationFiltersItem.Index = 6;
            this.simpleSkeletonizationFiltersItem.Text = "Simple &Skeletonization";
            this.simpleSkeletonizationFiltersItem.Click += new System.EventHandler(this.simpleSkeletonizationFiltersItem_Click);
            // 
            // shrinkFiltersItem
            // 
            this.shrinkFiltersItem.Index = 7;
            this.shrinkFiltersItem.Text = "Shrink";
            this.shrinkFiltersItem.Click += new System.EventHandler(this.shrinkFiltersItem_Click);
            // 
            // menuItem30
            // 
            this.menuItem30.Index = 8;
            this.menuItem30.Text = "-";
            // 
            // labelingFiltersItem
            // 
            this.labelingFiltersItem.Index = 9;
            this.labelingFiltersItem.Text = "Connected Components Labeling";
            this.labelingFiltersItem.Click += new System.EventHandler(this.labelingFiltersItem_Click);
            // 
            // blobExtractorFiltersItem
            // 
            this.blobExtractorFiltersItem.Index = 10;
            this.blobExtractorFiltersItem.Text = "&Blob Extractor";
            this.blobExtractorFiltersItem.Click += new System.EventHandler(this.blobExtractorFiltersItem_Click);
            // 
            // menuItem23
            // 
            this.menuItem23.Index = 8;
            this.menuItem23.Text = "-";
            // 
            // resizeFiltersItem
            // 
            this.resizeFiltersItem.Index = 9;
            this.resizeFiltersItem.Text = "&Resize";
            this.resizeFiltersItem.Click += new System.EventHandler(this.resizeFiltersItem_Click);
            // 
            // rotateFiltersItem
            // 
            this.rotateFiltersItem.Index = 10;
            this.rotateFiltersItem.Text = "Ro&tate";
            this.rotateFiltersItem.Click += new System.EventHandler(this.rotateFiltersItem_Click);
            // 
            // menuItem26
            // 
            this.menuItem26.Index = 11;
            this.menuItem26.Text = "-";
            // 
            // levelsFiltersItem
            // 
            this.levelsFiltersItem.Index = 12;
            this.levelsFiltersItem.Shortcut = System.Windows.Forms.Shortcut.CtrlL;
            this.levelsFiltersItem.Text = "&Levels";
            this.levelsFiltersItem.Click += new System.EventHandler(this.levelsFiltersItem_Click);
            // 
            // medianFiltersItem
            // 
            this.medianFiltersItem.Index = 13;
            this.medianFiltersItem.Text = "Me&dian";
            this.medianFiltersItem.Click += new System.EventHandler(this.medianFiltersItem_Click);
            // 
            // gammaFiltersItem
            // 
            this.gammaFiltersItem.Index = 14;
            this.gammaFiltersItem.Text = "&Gamma Correction";
            this.gammaFiltersItem.Click += new System.EventHandler(this.gammaFiltersItem_Click);
            // 
            // menuItem25
            // 
            this.menuItem25.Index = 15;
            this.menuItem25.Text = "-";
            // 
            // fourierFiltersItem
            // 
            this.fourierFiltersItem.Index = 16;
            this.fourierFiltersItem.Text = "&Fourier Transformation";
            this.fourierFiltersItem.Click += new System.EventHandler(this.fourierFiltersItem_Click);
            // 
            // ContextMenuC
            // 
            this.ContextMenuC.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripMenuItem1,
            this.toolStripSeparator5,
            this.toolStripMenuItem2,
            this.toolStripMenuItem3});
            this.ContextMenuC.Name = "ScrollBarContextMenu";
            this.ContextMenuC.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
            this.ContextMenuC.Size = new System.Drawing.Size(176, 76);
            // 
            // toolStripMenuItem1
            // 
            this.toolStripMenuItem1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.toolStripMenuItem1.Name = "toolStripMenuItem1";
            this.toolStripMenuItem1.Size = new System.Drawing.Size(175, 22);
            this.toolStripMenuItem1.Text = "Delete &This Slice";
            this.toolStripMenuItem1.TextImageRelation = System.Windows.Forms.TextImageRelation.Overlay;
            this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_Click);
            // 
            // toolStripSeparator5
            // 
            this.toolStripSeparator5.Name = "toolStripSeparator5";
            this.toolStripSeparator5.Size = new System.Drawing.Size(172, 6);
            // 
            // toolStripMenuItem2
            // 
            this.toolStripMenuItem2.Name = "toolStripMenuItem2";
            this.toolStripMenuItem2.Size = new System.Drawing.Size(175, 22);
            this.toolStripMenuItem2.Text = "&Delete Slices &Before";
            this.toolStripMenuItem2.Click += new System.EventHandler(this.toolStripMenuItem2_Click);
            // 
            // toolStripMenuItem3
            // 
            this.toolStripMenuItem3.Name = "toolStripMenuItem3";
            this.toolStripMenuItem3.Size = new System.Drawing.Size(175, 22);
            this.toolStripMenuItem3.Text = "&Delete Slices &After";
            this.toolStripMenuItem3.Click += new System.EventHandler(this.toolStripMenuItem3_Click);
            // 
            // ContextMenuT
            // 
            this.ContextMenuT.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.deleteThisSliceToolStripMenuItem,
            this.toolStripSeparator4,
            this.deleteSlicesBeforeToolStripMenuItem,
            this.deleteSlicesAfterToolStripMenuItem});
            this.ContextMenuT.Name = "ScrollBarContextMenu";
            this.ContextMenuT.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
            this.ContextMenuT.Size = new System.Drawing.Size(176, 76);
            // 
            // deleteThisSliceToolStripMenuItem
            // 
            this.deleteThisSliceToolStripMenuItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.deleteThisSliceToolStripMenuItem.Name = "deleteThisSliceToolStripMenuItem";
            this.deleteThisSliceToolStripMenuItem.Size = new System.Drawing.Size(175, 22);
            this.deleteThisSliceToolStripMenuItem.Text = "Delete &This Slice";
            this.deleteThisSliceToolStripMenuItem.TextImageRelation = System.Windows.Forms.TextImageRelation.Overlay;
            this.deleteThisSliceToolStripMenuItem.Click += new System.EventHandler(this.deleteThisSliceToolStripMenuItem_Click);
            // 
            // toolStripSeparator4
            // 
            this.toolStripSeparator4.Name = "toolStripSeparator4";
            this.toolStripSeparator4.Size = new System.Drawing.Size(172, 6);
            // 
            // deleteSlicesBeforeToolStripMenuItem
            // 
            this.deleteSlicesBeforeToolStripMenuItem.Name = "deleteSlicesBeforeToolStripMenuItem";
            this.deleteSlicesBeforeToolStripMenuItem.Size = new System.Drawing.Size(175, 22);
            this.deleteSlicesBeforeToolStripMenuItem.Text = "&Delete Slices &Before";
            this.deleteSlicesBeforeToolStripMenuItem.Click += new System.EventHandler(this.deleteSlicesBeforeToolStripMenuItem_Click);
            // 
            // deleteSlicesAfterToolStripMenuItem
            // 
            this.deleteSlicesAfterToolStripMenuItem.Name = "deleteSlicesAfterToolStripMenuItem";
            this.deleteSlicesAfterToolStripMenuItem.Size = new System.Drawing.Size(175, 22);
            this.deleteSlicesAfterToolStripMenuItem.Text = "&Delete Slices &After";
            this.deleteSlicesAfterToolStripMenuItem.Click += new System.EventHandler(this.deleteSlicesAfterToolStripMenuItem_Click);
            // 
            // ContextMenuS
            // 
            this.ContextMenuS.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripMenuItem4,
            this.toolStripSeparator6,
            this.toolStripMenuItem5,
            this.toolStripMenuItem6});
            this.ContextMenuS.Name = "ScrollBarContextMenu";
            this.ContextMenuS.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
            this.ContextMenuS.Size = new System.Drawing.Size(176, 76);
            // 
            // toolStripMenuItem4
            // 
            this.toolStripMenuItem4.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.toolStripMenuItem4.Name = "toolStripMenuItem4";
            this.toolStripMenuItem4.Size = new System.Drawing.Size(175, 22);
            this.toolStripMenuItem4.Text = "Delete &This Slice";
            this.toolStripMenuItem4.TextImageRelation = System.Windows.Forms.TextImageRelation.Overlay;
            this.toolStripMenuItem4.Click += new System.EventHandler(this.toolStripMenuItem4_Click);
            // 
            // toolStripSeparator6
            // 
            this.toolStripSeparator6.Name = "toolStripSeparator6";
            this.toolStripSeparator6.Size = new System.Drawing.Size(172, 6);
            // 
            // toolStripMenuItem5
            // 
            this.toolStripMenuItem5.Name = "toolStripMenuItem5";
            this.toolStripMenuItem5.Size = new System.Drawing.Size(175, 22);
            this.toolStripMenuItem5.Text = "&Delete Slices &Before";
            this.toolStripMenuItem5.Click += new System.EventHandler(this.toolStripMenuItem5_Click);
            // 
            // toolStripMenuItem6
            // 
            this.toolStripMenuItem6.Name = "toolStripMenuItem6";
            this.toolStripMenuItem6.Size = new System.Drawing.Size(175, 22);
            this.toolStripMenuItem6.Text = "&Delete Slices &After";
            this.toolStripMenuItem6.Click += new System.EventHandler(this.toolStripMenuItem6_Click);
            // 
            // RightClickMenu
            // 
            this.RightClickMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem49,
            this.autoFOV,
            this.menuItem61,
            this.menuItem54,
            this.menuItem48,
            this.imageItem,
            this.filtersItem,
            this.menuItem35,
            this.menuItem13,
            this.menuItem32,
            this.menuItem42,
            this.menuItem43,
            this.menuItem44,
            this.menuItem60,
            this.menuItem47,
            this.menuItem50,
            this.menuItem51});
            this.RightClickMenu.Popup += new System.EventHandler(this.RightClickMenu_Popup);
            // 
            // menuItem49
            // 
            this.menuItem49.Checked = true;
            this.menuItem49.Index = 0;
            this.menuItem49.Text = "&Hot Tracking";
            this.menuItem49.Click += new System.EventHandler(this.menuItem49_Click);
            // 
            // autoFOV
            // 
            this.autoFOV.Checked = true;
            this.autoFOV.Index = 1;
            this.autoFOV.Text = "&Auto FOV based resize";
            this.autoFOV.Click += new System.EventHandler(this.menuItem61_Click);
            // 
            // menuItem61
            // 
            this.menuItem61.Checked = true;
            this.menuItem61.Index = 2;
            this.menuItem61.Text = "Auto Normalize and Contrast";
            this.menuItem61.Click += new System.EventHandler(this.menuItem61_Click_1);
            // 
            // menuItem54
            // 
            this.menuItem54.Index = 3;
            this.menuItem54.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem55,
            this.menuItem56,
            this.menuItem57,
            this.menuItem58,
            this.menuItem59});
            this.menuItem54.Text = "Image &Smoothing Options";
            // 
            // menuItem55
            // 
            this.menuItem55.Checked = true;
            this.menuItem55.Index = 0;
            this.menuItem55.RadioCheck = true;
            this.menuItem55.Text = "&Nearest Neighbour";
            this.menuItem55.Click += new System.EventHandler(this.menuItem55_Click);
            // 
            // menuItem56
            // 
            this.menuItem56.Index = 1;
            this.menuItem56.RadioCheck = true;
            this.menuItem56.Text = "&Bi-Linear";
            this.menuItem56.Click += new System.EventHandler(this.menuItem56_Click);
            // 
            // menuItem57
            // 
            this.menuItem57.Index = 2;
            this.menuItem57.RadioCheck = true;
            this.menuItem57.Text = "&High Quality Bi-Linear";
            this.menuItem57.Click += new System.EventHandler(this.menuItem57_Click);
            // 
            // menuItem58
            // 
            this.menuItem58.Index = 3;
            this.menuItem58.RadioCheck = true;
            this.menuItem58.Shortcut = System.Windows.Forms.Shortcut.Ins;
            this.menuItem58.Text = "Bi-Cubic";
            this.menuItem58.Click += new System.EventHandler(this.menuItem58_Click);
            // 
            // menuItem59
            // 
            this.menuItem59.Index = 4;
            this.menuItem59.RadioCheck = true;
            this.menuItem59.Text = "&High Quality Bicubic";
            this.menuItem59.Click += new System.EventHandler(this.menuItem59_Click);
            // 
            // menuItem48
            // 
            this.menuItem48.Index = 4;
            this.menuItem48.Text = "-";
            // 
            // menuItem35
            // 
            this.menuItem35.Index = 7;
            this.menuItem35.Text = "-";
            // 
            // menuItem13
            // 
            this.menuItem13.Index = 8;
            this.menuItem13.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem36,
            this.menuItem37,
            this.menuItem38});
            this.menuItem13.Text = "Apply Filter to Display Volume";
            this.menuItem13.Click += new System.EventHandler(this.menuItem13_Click);
            // 
            // menuItem36
            // 
            this.menuItem36.Index = 0;
            this.menuItem36.RadioCheck = true;
            this.menuItem36.Text = "Apply As &Transverse";
            this.menuItem36.Click += new System.EventHandler(this.menuItem36_Click);
            // 
            // menuItem37
            // 
            this.menuItem37.Index = 1;
            this.menuItem37.RadioCheck = true;
            this.menuItem37.Text = "Apply as &Coronal";
            this.menuItem37.Click += new System.EventHandler(this.menuItem37_Click);
            // 
            // menuItem38
            // 
            this.menuItem38.Index = 2;
            this.menuItem38.RadioCheck = true;
            this.menuItem38.Text = "Apply as &Sagittal";
            this.menuItem38.Click += new System.EventHandler(this.menuItem38_Click);
            // 
            // menuItem32
            // 
            this.menuItem32.Index = 9;
            this.menuItem32.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem39,
            this.menuItem40,
            this.menuItem41});
            this.menuItem32.RadioCheck = true;
            this.menuItem32.Text = "Apply Filter to Data + Display Volume";
            this.menuItem32.Click += new System.EventHandler(this.menuItem32_Click);
            // 
            // menuItem39
            // 
            this.menuItem39.Index = 0;
            this.menuItem39.RadioCheck = true;
            this.menuItem39.Text = "Apply As &Transverse";
            this.menuItem39.Click += new System.EventHandler(this.menuItem39_Click);
            // 
            // menuItem40
            // 
            this.menuItem40.Index = 1;
            this.menuItem40.RadioCheck = true;
            this.menuItem40.Text = "Apply as &Coronal";
            this.menuItem40.Click += new System.EventHandler(this.menuItem40_Click);
            // 
            // menuItem41
            // 
            this.menuItem41.Index = 2;
            this.menuItem41.RadioCheck = true;
            this.menuItem41.Text = "Apply as &Sagittal";
            this.menuItem41.Click += new System.EventHandler(this.menuItem41_Click);
            // 
            // menuItem42
            // 
            this.menuItem42.Index = 10;
            this.menuItem42.Text = "-";
            // 
            // menuItem43
            // 
            this.menuItem43.Index = 11;
            this.menuItem43.Text = "Subject Header &Information";
            this.menuItem43.Click += new System.EventHandler(this.menuItem43_Click);
            // 
            // menuItem44
            // 
            this.menuItem44.Index = 12;
            this.menuItem44.Text = "-";
            // 
            // menuItem60
            // 
            this.menuItem60.Index = 13;
            this.menuItem60.Text = "3D Render";
            this.menuItem60.Click += new System.EventHandler(this.menuItem60_Click_1);
            // 
            // menuItem47
            // 
            this.menuItem47.Index = 14;
            this.menuItem47.Text = "2D Render";
            this.menuItem47.Click += new System.EventHandler(this.menuItem47_Click);
            // 
            // menuItem50
            // 
            this.menuItem50.Index = 15;
            this.menuItem50.Text = "-";
            // 
            // menuItem51
            // 
            this.menuItem51.Index = 16;
            this.menuItem51.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem52,
            this.menuItem53});
            this.menuItem51.Text = "&Save current dataset as";
            // 
            // menuItem52
            // 
            this.menuItem52.Index = 0;
            this.menuItem52.Text = "&XML";
            this.menuItem52.Click += new System.EventHandler(this.menuItem52_Click);
            // 
            // menuItem53
            // 
            this.menuItem53.Index = 1;
            this.menuItem53.Text = "&Analyze (SPM) Data";
            this.menuItem53.Click += new System.EventHandler(this.menuItem53_Click);
            // 
            // sbar
            // 
            this.sbar.BackColor = System.Drawing.Color.Black;
            this.sbar.ContextMenuStrip = this.ContextMenuS;
            this.sbar.Dock = System.Windows.Forms.DockStyle.Right;
            this.sbar.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.sbar.LargeChange = 20;
            this.sbar.Location = new System.Drawing.Point(494, 0);
            this.sbar.Maximum = 100;
            this.sbar.Name = "sbar";
            this.sbar.Orientation = System.Windows.Forms.Orientation.Vertical;
            this.sbar.ShowMaximumLabel = true;
            this.sbar.ShowMinimumLabel = true;
            this.sbar.ShowValueLabel = true;
            this.sbar.Size = new System.Drawing.Size(34, 362);
            this.sbar.TabIndex = 6;
            this.sbar.TickFrequency = 10;
            this.sbar.ValueChanged += new System.EventHandler(this.sbar_ValueChanged);
            this.sbar.Scroll += new System.EventHandler(this.sbar_Scroll);
            // 
            // cbar
            // 
            this.cbar.BackColor = System.Drawing.Color.Black;
            this.cbar.ContextMenuStrip = this.ContextMenuC;
            this.cbar.Dock = System.Windows.Forms.DockStyle.Left;
            this.cbar.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cbar.LargeChange = 20;
            this.cbar.Location = new System.Drawing.Point(0, 0);
            this.cbar.Maximum = 100;
            this.cbar.Name = "cbar";
            this.cbar.Orientation = System.Windows.Forms.Orientation.Vertical;
            this.cbar.ShowMaximumLabel = true;
            this.cbar.ShowMinimumLabel = true;
            this.cbar.ShowValueLabel = true;
            this.cbar.Size = new System.Drawing.Size(34, 362);
            this.cbar.TabIndex = 7;
            this.cbar.TickFrequency = 10;
            this.cbar.ValueChanged += new System.EventHandler(this.cbar_ValueChanged);
            this.cbar.Scroll += new System.EventHandler(this.cbar_Scroll);
            // 
            // tbar
            // 
            this.tbar.BackColor = System.Drawing.Color.Black;
            this.tbar.ContextMenuStrip = this.ContextMenuT;
            this.tbar.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.tbar.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbar.LargeChange = 20;
            this.tbar.Location = new System.Drawing.Point(0, 362);
            this.tbar.Maximum = 100;
            this.tbar.Name = "tbar";
            this.tbar.ShowMaximumLabel = true;
            this.tbar.ShowMinimumLabel = true;
            this.tbar.ShowValueLabel = true;
            this.tbar.Size = new System.Drawing.Size(528, 34);
            this.tbar.TabIndex = 8;
            this.tbar.TickFrequency = 10;
            this.tbar.ValueChanged += new System.EventHandler(this.tbar_ValueChanged);
            this.tbar.Scroll += new System.EventHandler(this.tbar_Scroll);
            // 
            // ImageDoc
            // 
            this.AllowedStates = WeifenLuo.WinFormsUI.ContentStates.Document;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
            this.AutoScroll = true;
            this.AutoValidate = System.Windows.Forms.AutoValidate.Disable;
            this.BackColor = System.Drawing.Color.Black;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.ClientSize = new System.Drawing.Size(528, 396);
            this.Controls.Add(this.sbar);
            this.Controls.Add(this.cbar);
            this.Controls.Add(this.tbar);
            this.Cursor = System.Windows.Forms.Cursors.Cross;
            this.DoubleBuffered = true;
            this.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ForeColor = System.Drawing.Color.WhiteSmoke;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "ImageDoc";
            this.ShowHint = WeifenLuo.WinFormsUI.DockState.Document;
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.Text = "Image";
            this.Load += new System.EventHandler(this.ImageDoc_Load);
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ImageDoc_MouseUp);
            this.Scroll += new System.Windows.Forms.ScrollEventHandler(this.ImageDoc_Scroll);
            this.DoubleClick += new System.EventHandler(this.ImageDoc_MouseClick);
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ImageDoc_Closed);
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ImageDoc_MouseDown);
            this.MouseLeave += new System.EventHandler(this.ImageDoc_MouseLeave);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.ImageDoc_MouseMove);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ImageDoc_KeyDown);
            this.ContextMenuC.ResumeLayout(false);
            this.ContextMenuT.ResumeLayout(false);
            this.ContextMenuS.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.sbar)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.cbar)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.tbar)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        // Init the document
        private void Init()
        {
            // init components
            InitializeComponent();

            // form style
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw, true);

            // init scroll bars
            this.AutoScroll = true;

            UpdateSize();
            this.cbar.Value = this.cbar.Value / 2;
        }

        // Execute command
        public void ExecuteCommand(ImageDocCommands cmd)
        {
            switch (cmd)
            {
                case ImageDocCommands.Clone:		// clone the image
                    Clone();
                    break;
                case ImageDocCommands.Crop:			// crop the image
                    Crop();
                    break;
                case ImageDocCommands.ZoomIn:		// zoom in
                    ZoomIn();
                    break;
                case ImageDocCommands.ZoomOut:		// zoom out
                    ZoomOut();
                    break;
                case ImageDocCommands.ZoomOriginal:	// original size
                    zoom = 1;
                    UpdateZoom();
                    break;
                case ImageDocCommands.FitToSize:	// fit to screen
                    FitToScreen();
                    break;
                case ImageDocCommands.Levels:		// levels
                    Levels();
                    break;
                case ImageDocCommands.Grayscale:	// grayscale
                    Grayscale();
                    break;
                case ImageDocCommands.Threshold:	// threshold
                    Threshold();
                    break;
                case ImageDocCommands.Morphology:	// morphology
                    Morphology();
                    break;
                case ImageDocCommands.Convolution:	// convolution
                    Convolution();
                    break;
                case ImageDocCommands.Resize:		// resize the image
                    ResizeImage();
                    break;
                case ImageDocCommands.Rotate:		// rotate the image
                    RotateImage();
                    break;
                case ImageDocCommands.Brightness:	// adjust brightness
                    Brightness();
                    break;
                case ImageDocCommands.Contrast:		// modify contrast
                    Contrast();
                    break;
                case ImageDocCommands.Saturation:	// adjust saturation
                    Saturation();
                    break;
                case ImageDocCommands.Fourier:		// fourier transformation
                    ForwardFourierTransformation();
                    break;
            }
        }

        // Update document and notify client about changes
        public void UpdateNewImage()
        {
            // format image
            //if ((image.PixelFormat != PixelFormat.Format24bppRgb) || (image.PixelFormat != PixelFormat.Format8bppIndexed))
                //{AForge.Imaging.Image.FormatImage(ref image);}
           
            // update size
            UpdateSize();
            // repaint
            Invalidate();

            // notify host
            if (DocumentChanged != null)
                DocumentChanged(this, null);
        }

        // Reload image from file
        public void Reload()
        {
            if (fileName != null)
            {
                try
                {
                    // load image
                    Bitmap newImage = (Bitmap)Bitmap.FromFile(fileName);

                    // Release current image
                    image.Dispose();
                    // set document image to just loaded
                    image = newImage;

                }
                catch (Exception)
                {
                    throw new ApplicationException("Failed reloading image");
                }

                // update
                UpdateNewImage();
            
            }
        }

        // Center image in the document
        public void Center()
        {
            Rectangle rc = ClientRectangle;
            Point p = this.AutoScrollPosition;
            int width = (int)(this.width * zoom);
            int height = (int)(this.height * zoom);

            if (rc.Width < width)
                p.X = (width - rc.Width) >> 1;
            if (rc.Height < height)
                p.Y = (height - rc.Height) >> 1;

            this.AutoScrollPosition = p;
        }

        // Update document size 
        private void UpdateSize()
        {
            // image dimension
            width = image.Width;
            height = image.Height;

            // scroll bar size
            this.AutoScrollMinSize = new Size((int)(width * zoom), (int)(height * zoom));
        }

        // Paint image
        protected override void OnPaint(PaintEventArgs e)
        {
            if (image != null)
            {

                // get selection end point
                GetImageAndScreenPoints(new Point(mousePosition.X, mousePosition.Y), out end, out endW);

                Graphics g = e.Graphics;
                Rectangle rc = ClientRectangle;
                Pen pen = new Pen(Color.FromArgb(0, 0, 0));

                int width = (int)(this.width * zoom);
                int height = (int)(this.height * zoom);
                int x = (rc.Width < width) ? this.AutoScrollPosition.X : (rc.Width - width) / 2;
                int y = (rc.Height < height) ? this.AutoScrollPosition.Y : (rc.Height - height) / 2;

                // draw rectangle around the image
                g.DrawRectangle(new Pen(Color.Black,1), x - 1, y - 1, width + 1, height + 1);
                
                Point temp = mousePosition;
                Color old;
                
                // set nearest neighbor interpolation to avoid image smoothing
                g.InterpolationMode = InterpolationMode.NearestNeighbor;
                if (menuItem56.Checked) { g.InterpolationMode = InterpolationMode.Bilinear; }
                if (menuItem57.Checked) { g.InterpolationMode = InterpolationMode.HighQualityBilinear; }
                if (menuItem58.Checked) { g.InterpolationMode = InterpolationMode.Bicubic; }
                if (menuItem59.Checked) { g.InterpolationMode = InterpolationMode.HighQualityBicubic; }

                // for grayscale level filtering on right mouse drag
                image = (Bitmap)filter.Apply(new Bitmap(image));
                
                if (menuItem61.Checked) image = contrast_Stretch(image, max,min);
				        
                // draw image
                if (temp.X < image.Size.Width - 2 && temp.Y < image.Size.Height - 2 && this.menuItem49.Checked == true && temp.X > 2 && temp.Y > 2)
                {
                    old = image.GetPixel(temp.X, temp.Y);
                    image.SetPixel(temp.X, temp.Y, Color.FromArgb(255, 0, 0));
                    g.DrawImage(image, x, y, width, height);
                    image.SetPixel(temp.X, temp.Y, old);
                }
                else
                {
                    g.DrawImage(image, x, y, width, height);                
                }
                switch (this.currentType)
                {
                    case "t":
                        {
                            g.DrawString("R", new Font("Verdana", 10), Brushes.Wheat, x-20, y + ((height - 15) / 2));
                            g.DrawString("L", new Font("Verdana", 10), Brushes.Wheat, x + ((width - 13))+20, y + ((height - 15) / 2));
                            g.DrawString("A", new Font("Verdana", 10), Brushes.Wheat, x + (width - 15) / 2, y - 20);
                            g.DrawString("P", new Font("Verdana", 10), Brushes.Wheat, x + (width - 15) / 2, y + (height - 15)+20);
                            break;
                        }
                    case "c":
                        {
                            g.DrawString("R", new Font("Verdana", 10), Brushes.Wheat, x-20, y + ((height - 15) / 2));
                            g.DrawString("L", new Font("Verdana", 10), Brushes.Wheat, x + ((width - 13))+20, y + ((height - 15) / 2));
                            g.DrawString("U", new Font("Verdana", 10), Brushes.Wheat, x + (width - 15) / 2, y -20);
                            g.DrawString("D", new Font("Verdana", 10), Brushes.Wheat, x + (width - 15) / 2, y + (height - 15+20));
                            break;
                        }
                    case "s":
                        {
                            g.DrawString("P", new Font("Verdana", 10), Brushes.Wheat, x-20, y + ((height - 15) / 2));
                            g.DrawString("A", new Font("Verdana", 10), Brushes.Wheat, x + ((width - 13))+20, y + ((height - 15) / 2));
                            g.DrawString("U", new Font("Verdana", 10), Brushes.Wheat, x + (width - 15) / 2, y -20);
                            g.DrawString("D", new Font("Verdana", 10), Brushes.Wheat, x + (width - 15) / 2, y + (height - 15)+20);
                            break;
                        }
                }
                pen.Dispose();
            }
        }

        // Mouse click
        protected override void OnClick(EventArgs e)
        {
            Focus();
        }

        // Apply filter on the image
        private void ApplyFilter(IFilter filter)
        {
            try
            {

                if (this.IsColorMatrixLoaded == true)
                {
                    MessageBox.Show("Filters cannot be applied to these matrices");
                    return;
                }

                // set wait cursor
                this.Cursor = Cursors.WaitCursor;

                // apply filter to the image

                if (menuItem36.Checked == true)
                {
                    Bitmap temp;
                    temp = TransverseSlice(1);
                    temp = filter.Apply(temp);
                    datamatrix = new Int16[temp.Width + 1, temp.Height + 1, this.z + 1];

                    for (int k = 0; k < this.z; k++)
                    {
                        //apply to all transverse slices
                        temp = TransverseSlice(k);
                        temp = filter.Apply(temp);
                        for (int i = 0; i < temp.Height; i++)
                            for (int j = 0; j < temp.Width; j++)
                                datamatrix[i, j, k] = (Int16)temp.GetPixel(i, j).R;
                    }
                    //MessageBox.Show("1");
                }

                if (menuItem37.Checked == true)
                {
                    Bitmap temp;
                    temp = CoronalSlice(1);
                    temp = filter.Apply(temp);
                    datamatrix = new Int16[temp.Width + 1, this.y + 1, temp.Height + 1];
                    int z = temp.Height + 1; //used later

                    for (int j = 0; j < this.y; j++)
                    {
                        //apply to all coronal slices
                        temp = CoronalSlice(j);
                        temp = filter.Apply(temp);
                        for (int i = 0; i < temp.Width; i++)
                            for (int k = 0; k < temp.Height; k++)
                                datamatrix[i, j, z - 1 - k] = (Int16)temp.GetPixel(i, k).R;
                    }
                    //MessageBox.Show("2");
                }

                if (menuItem38.Checked == true)
                {
                    Bitmap temp;
                    temp = SagittalSlice(1);
                    temp = filter.Apply(temp);
                    datamatrix = new Int16[this.x + 1, temp.Width + 1, temp.Height + 1];
                    int y = temp.Width + 1;
                    int z = temp.Height + 1;

                    for (int i = 0; i < this.x; i++)
                    {
                        //apply to all sagittal slices
                        temp = SagittalSlice(i);
                        temp = filter.Apply(temp);
                        for (int j = 0; j < temp.Width; j++)
                            for (int k = 0; k < temp.Height; k++)
                                datamatrix[i, y - 1 - j, z - 1 - k] = (Int16)temp.GetPixel(j, k).R;
                    }
                    //MessageBox.Show("3");
                }

                //apply to original dataset
                //Raiseevent & Get original Data

                //Then Apply Filter to this Data
                //
                if (menuItem39.Checked == true)
                {
                    Bitmap temp;
                    temp = TransverseSlice(1);
                    temp = filter.Apply(temp);
                    tempdatamatrix = new Int16[temp.Width + 1, temp.Height + 1, this.z + 1];
                    for (int k = 0; k < this.z; k++)
                    {
                        //apply to all transverse slices
                        temp = TransverseSlice(k);
                        temp = filter.Apply(temp);
                        for (int i = 0; i < temp.Width; i++)
                            for (int j = 0; j < temp.Height; j++)
                                tempdatamatrix[i, j, k] = (Int16)temp.GetPixel(i, j).R;
                    }
                    BaseDatasetChanged(tempdatamatrix, EventArgs.Empty);
                    //MessageBox.Show("4");                            

                }

                if (menuItem40.Checked == true)
                {
                    Bitmap temp;
                    temp = CoronalSlice(1);
                    temp = filter.Apply(temp);
                    tempdatamatrix = new Int16[temp.Width + 1, this.y + 1, temp.Height + 1];
                    int z = temp.Height + 1;

                    for (int j = 0; j < this.y; j++)
                    {
                        //apply to all coronal slices
                        temp = CoronalSlice(j);
                        temp = filter.Apply(temp);
                        for (int i = 0; i < temp.Width; i++)
                            for (int k = 0; k < temp.Height; k++)
                                tempdatamatrix[i, j, z - 1 - k] = (Int16)temp.GetPixel(i, k).R;
                    }
                    BaseDatasetChanged(tempdatamatrix, EventArgs.Empty);
                    //MessageBox.Show("5");

                }

                if (menuItem41.Checked == true)
                {
                    Bitmap temp;
                    temp = SagittalSlice(1);
                    temp = filter.Apply(temp);
                    tempdatamatrix = new Int16[this.x + 1, temp.Width + 1, temp.Height + 1];
                    int y = temp.Width + 1;
                    int z = temp.Height + 1;

                    for (int i = 0; i < this.x; i++)
                    {
                        //apply to all sagittal slices
                        temp = SagittalSlice(i);
                        temp = filter.Apply(temp);
                        for (int j = 0; j < temp.Width; j++)
                            for (int k = 0; k < temp.Height; k++)
                                tempdatamatrix[i, y - 1 - j, z - 1 - k] = (Int16)temp.GetPixel(j, k).R;
                    }
                    BaseDatasetChanged(tempdatamatrix, EventArgs.Empty);
                    //MessageBox.Show("6");
                }

                Bitmap newImage = filter.Apply(image);

                if (host.CreateNewDocumentOnChange)
                {
                    // open new image in new document
                    host.NewDocument(newImage);
                }
                else
                {
                    if (host.RememberOnChange)
                    {
                        // backup current image
                        if (backup != null)
                            backup.Dispose();

                        backup = image;
                    }
                    else
                    {
                        // release current image
                        image.Dispose();
                    }

                    image = newImage;

                    // update
                    UpdateNewImage();
                    
                }
            }
            catch (ArgumentException e)
            {
                MessageBox.Show("Selected filter can not be applied to the image", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(e.Message.ToString());
            }
            finally
            {
                // restore cursor
                this.Cursor = Cursors.Default;
            }
        }

        // on "Image" item popup
        private void imageItem_Popup(object sender, System.EventArgs e)
        {
            this.backImageItem.Enabled = (backup != null);
            this.cropImageItem.Checked = cropping;
        }

        // Restore image to previous
        private void backImageItem_Click(object sender, System.EventArgs e)
        {
            if (backup != null)
            {
                // release current image
                image.Dispose();
                // restore
                image = backup;
                backup = null;

                // update
                UpdateNewImage();
                // format image
                AForge.Imaging.Image.FormatImage(ref image);
           
            }
        }

        // Clone the image
        private void Clone()
        {
            if (host != null)
            {
                Bitmap clone = AForge.Imaging.Image.Clone(image);

                if (!host.NewDocument(clone))
                {
                    clone.Dispose();
                }
            }
        }

        // On "Image->Clone" item click
        private void cloneImageItem_Click(object sender, System.EventArgs e)
        {
            Clone();
        }

        // Update zoom factor
        private void UpdateZoom()
        {
            this.AutoScrollMinSize = new Size((int)(width * zoom), (int)(height * zoom));
            this.Invalidate();

            // notify host
            if (ZoomChanged != null)
                ZoomChanged(this, null);
        }

        // Zoom image
        private void zoomItem_Click(object sender, System.EventArgs e)
        {
            // get menu item text
            String t = ((MenuItem)sender).Text;
            // parse it`s value
            int i = int.Parse(t.Remove(t.Length - 1, 1));
            // calc zoom factor
            zoom = (float)i / 100;

            UpdateZoom();
        }

        public void ChangeZoomManually(float z)
        {
            zoom = z;
            UpdateZoom();
        }

        // Zoom In image
        private void ZoomIn()
        {
            float z = zoom * 1.5f;

            if (z <= 10)
            {
                zoom = z;
                UpdateZoom();
            }
        }

        // On "Image->Zoom->Zoom In" item click
        private void zoomInImageItem_Click(object sender, System.EventArgs e)
        {
            ZoomIn();
        }

        // Zoom Out image
        private void ZoomOut()
        {
            float z = zoom / 1.5f;

            if (z >= 0.05)
            {
                zoom = z;
                UpdateZoom();
            }
        }

        // On "Image->Zoom->Zoom out" item click
        private void zoomOutImageItem_Click(object sender, System.EventArgs e)
        {
            ZoomOut();
        }

        // Fit to size
        private void FitToScreen()
        {
            Rectangle rc = ClientRectangle;

            zoom = Math.Min(1,Math.Min((float)rc.Width / (width + 2), (float)rc.Height / (height + 2)));

            UpdateZoom();
        }

        // On "Image->Zoom->Fit To Screen" item click
        private void zoomFitImageItem_Click(object sender, System.EventArgs e)
        {
            FitToScreen();
        }

        // Flip image
        private void flipImageItem_Click(object sender, System.EventArgs e)
        {
            image.RotateFlip(RotateFlipType.RotateNoneFlipY);

            Invalidate();
        }

        // Mirror image
        private void mirrorItem_Click(object sender, System.EventArgs e)
        {
            image.RotateFlip(RotateFlipType.RotateNoneFlipX);

            Invalidate();
        }

        // Rotate image 90 degree
        private void rotateImageItem_Click(object sender, System.EventArgs e)
        {
            image.RotateFlip(RotateFlipType.Rotate90FlipNone);

            // update
            UpdateNewImage();

        }

        // Invert image
        private void invertColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Invert());
        }

        // Rotatet colors
        private void rotateColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new RotateChannels());
        }

        // Sepia image
        private void sepiaColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Sepia());
        }

        // Grayscale image
        private void Grayscale()
        {
            if (image.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                MessageBox.Show("The image is already grayscale", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            ApplyFilter(new GrayscaleBT709());
        }

        // On "Filter->Color->Grayscale"
        private void grayscaleColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            Grayscale();
        }

        // Remove green and blue channels
        private void redColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new ChannelFiltering(new Range(0, 255), new Range(0, 0), new Range(0, 0)));
        }

        // Remove red and blue channels
        private void greenColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new ChannelFiltering(new Range(0, 0), new Range(0, 255), new Range(0, 0)));
        }

        // Remove red and green channels
        private void blueColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new ChannelFiltering(new Range(0, 0), new Range(0, 0), new Range(0, 255)));
        }

        // Remove green channel
        private void cyanColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new ChannelFiltering(new Range(0, 0), new Range(0, 255), new Range(0, 255)));
        }

        // Remove green channel
        private void magentaColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new ChannelFiltering(new Range(0, 255), new Range(0, 0), new Range(0, 255)));
        }

        // Remove blue channel
        private void yellowColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new ChannelFiltering(new Range(0, 255), new Range(0, 255), new Range(0, 0)));
        }

        // Color filtering
        private void colorFilteringColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            // check pixel format
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Color filtering can be applied to RGB images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            ColorFilteringForm form = new ColorFilteringForm();
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Euclidean color filtering
        private void euclideanFilteringColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            // check pixel format
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Euclidean color filtering can be applied to RGB images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            EuclideanColorFilteringForm form = new EuclideanColorFilteringForm();
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Channels filtering
        private void channelsFilteringColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            // check pixel format
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Channels filtering can be applied to RGB images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            ChannelFilteringForm form = new ChannelFilteringForm();
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Extract red channel of image
        private void extractRedColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new ExtractChannel(RGB.R));
        }

        // Extract green channel of image
        private void extractGreenColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new ExtractChannel(RGB.G));
        }

        // Extract blue channel of image
        private void extractRedBlueFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new ExtractChannel(RGB.B));
        }

        // Replace red channel
        private void replaceRedColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            // check pixel format
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Channels replacement can be applied to RGB images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            Bitmap channelImage = host.GetImage(this, "Select an image which will replace the red channel in the curren image", new Size(width, height), PixelFormat.Format8bppIndexed);

            if (channelImage != null)
                ApplyFilter(new ReplaceChannel(channelImage, RGB.R));
        }

        // Replace green channel
        private void replaceGreenColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            // check pixel format
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Channels replacement can be applied to RGB images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            Bitmap channelImage = host.GetImage(this, "Select an image which will replace the green channel in the curren image", new Size(width, height), PixelFormat.Format8bppIndexed);

            if (channelImage != null)
                ApplyFilter(new ReplaceChannel(channelImage, RGB.G));
        }

        // Replace blue channel
        private void replaceBlueColorFiltersItem_Click(object sender, System.EventArgs e)
        {
            // check pixel format
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Channels replacement can be applied to RGB images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            Bitmap channelImage = host.GetImage(this, "Select an image which will replace the blue channel in the curren image", new Size(width, height), PixelFormat.Format8bppIndexed);

            if (channelImage != null)
                ApplyFilter(new ReplaceChannel(channelImage, RGB.B));
        }

        // Adjust brighness using HSL
        private void Brightness()
        {
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Brightness filter using HSL color space is available for color images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            BrightnessForm form = new BrightnessForm();
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // On "Filters->HSL Color space->Brighness" menu item click
        private void brightnessHslFiltersItem_Click(object sender, System.EventArgs e)
        {
            Brightness();
        }

        // Modify contrast
        private void Contrast()
        {
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Contrast filter using HSL color space is available for color images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            ContrastForm form = new ContrastForm();
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // On "Filters->HSL Color space->Contrast" menu item click
        private void contrastHslFiltersItem_Click(object sender, System.EventArgs e)
        {
            Contrast();
        }

        // Adjust saturation using HSL
        private void Saturation()
        {
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Saturation filter using HSL color space is available for color images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            SaturationForm form = new SaturationForm();
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // On "Filters->HSL Color space->Saturation" menu item click
        private void saturationHslFiltersItem_Click(object sender, System.EventArgs e)
        {
            Saturation();
        }

        // HSL linear correction
        private void linearHslFiltersItem_Click(object sender, System.EventArgs e)
        {
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("HSL linear correction is available for color images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            HSLLinearForm form = new HSLLinearForm(new ImageStatistics(image, true,false,0,0));
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // HSL filtering
        private void filteringHslFiltersItem_Click(object sender, System.EventArgs e)
        {
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("HSL filtering is available for color images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            HSLFilteringForm form = new HSLFilteringForm();
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Hue modifier
        private void hueHslFiltersItem_Click(object sender, System.EventArgs e)
        {
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Hue modifier is available for color images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            HueModifierForm form = new HueModifierForm();
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Threshold binarization
        private void Threshold()
        {
            ThresholdForm form = new ThresholdForm();

            // set image to preview
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // On "Filters->Binarization->Threshold" menu item click
        private void thresholdBinaryFiltersItem_Click(object sender, System.EventArgs e)
        {
            Threshold();
        }

        // Threshold binarization with carry
        private void thresholdCarryBinaryFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new ThresholdCarry());
        }

        // Ordered dithering
        private void orderedDitherBinaryFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new OrderedDithering());
        }

        // Bayer ordered dithering
        private void bayerDitherBinaryFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new BayerDithering());
        }

        // Binarization using Floyd-Steinverg dithering algorithm
        private void floydBinaryFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new FloydSteinbergDithering());
        }

        // Binarization using Burkes dithering algorithm
        private void burkesBinaryFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new BurkesDithering());
        }

        // Binarization using Stucki dithering algorithm
        private void stuckiBinaryFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new StuckiDithering());
        }

        // Binarization using Jarvis, Judice and Ninke dithering algorithm
        private void jarvisBinaryFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new JarvisJudiceNinkeDithering());
        }

        // Binarization using Sierra dithering algorithm
        private void sierraBinaryFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new SierraDithering());
        }

        // Binarization using Stevenson and Arce dithering algorithm
        private void stevensonBinaryFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new StevensonArceDithering());
        }

        // Threshold using Simple Image Statistics
        private void sisThresholdBinaryFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new SISThreshold());
        }

        // Errosion (Mathematical Morphology)
        private void erosionMorphologyFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Erosion());
        }

        // Dilatation (Mathematical Morphology)
        private void dilatationMorphologyFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Dilatation());
        }

        // Opening (Mathematical Morphology)
        private void openingMorphologyFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Opening());
        }

        // Closing (Mathematical Morphology)
        private void closingMorphologyFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Closing());
        }

        // Custom morphology operator
        private void Morphology()
        {
            if (image.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                MessageBox.Show("Mathematical morpholgy filters can by applied to grayscale image only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            MathMorphologyForm form = new MathMorphologyForm(MathMorphologyForm.FilterTypes.Simple);
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // On "Filters->Morphology->Custom" menu item click
        private void customMorphologyFiltersItem_Click(object sender, System.EventArgs e)
        {
            Morphology();
        }

        // Hit & Miss mathematical morphology operator
        private void hitAndMissFiltersItem_Click(object sender, System.EventArgs e)
        {
            if (image.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                MessageBox.Show("Hit & Miss morpholgy filters can by applied to binary image only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            MathMorphologyForm form = new MathMorphologyForm(MathMorphologyForm.FilterTypes.HitAndMiss);
            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Mean
        private void meanConvolutionFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Mean());
        }

        // Blur
        private void blurConvolutionFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Blur());
        }

        // Gaussian smoothing
        private void gaussianConvolutionFiltersItem_Click(object sender, System.EventArgs e)
        {
            GaussianForm form = new GaussianForm();

            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Extended sharpening
        private void sharpenExConvolutionFiltersItem_Click(object sender, System.EventArgs e)
        {
            SharpenExForm form = new SharpenExForm();

            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Sharpen
        private void sharpenConvolutionFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Sharpen());
        }

        // Edges
        private void edgesConvolutionFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Edges());
        }

        // Custom convolution filter
        private void Convolution()
        {
            ConvolutionForm form = new ConvolutionForm();

            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // On "Filters->Convolution & Correlation->Custom" menu item click
        private void customConvolutionFiltersItem_Click(object sender, System.EventArgs e)
        {
            Convolution();
        }

        // Merge two images
        private void mergeTwosrcFiltersItem_Click(object sender, System.EventArgs e)
        {
            Bitmap overlayImage = host.GetImage(this, "Select an image to merge with the curren image", new Size(-1, -1), image.PixelFormat);

            if (overlayImage != null)
                ApplyFilter(new Merge(overlayImage));
        }

        // Intersect
        private void intersectTwosrcFiltersItem_Click(object sender, System.EventArgs e)
        {
            Bitmap overlayImage = host.GetImage(this, "Select an image to intersect with the curren image", new Size(-1, -1), image.PixelFormat);

            if (overlayImage != null)
                ApplyFilter(new Intersect(overlayImage));
        }

        // Add
        private void addTwosrcFiltersItem_Click(object sender, System.EventArgs e)
        {
            Bitmap overlayImage = host.GetImage(this, "Select an image to add to the curren image", new Size(-1, -1), image.PixelFormat);

            if (overlayImage != null)
                ApplyFilter(new Add(overlayImage));
        }

        // Subtract
        private void subtractTwosrcFiltersItem_Click(object sender, System.EventArgs e)
        {
            Bitmap overlayImage = host.GetImage(this, "Select an image to subtract from the curren image", new Size(-1, -1), image.PixelFormat);

            if (overlayImage != null)
                ApplyFilter(new Subtract(overlayImage));
        }

        // Difference
        private void differenceTwosrcFiltersItem_Click(object sender, System.EventArgs e)
        {
            Bitmap overlayImage = host.GetImage(this, "Select an image to get difference with the curren image", new Size(width, height), image.PixelFormat);

            if (overlayImage != null)
                ApplyFilter(new Difference(overlayImage));
        }

        // Move towards
        private void moveTowardsTwosrcFiltersItem_Click(object sender, System.EventArgs e)
        {
            Bitmap overlayImage = host.GetImage(this, "Select an image to which the curren image will be moved", new Size(width, height), image.PixelFormat);

            if (overlayImage != null)
                ApplyFilter(new MoveTowards(overlayImage, 10));
        }

        // Homogenity edge detector
        private void homogenityEdgeFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new HomogenityEdgeDetector());
        }

        // Difference edge detector
        private void differenceEdgeFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new DifferenceEdgeDetector());
        }

        // Sobel edge detector
        private void sobelEdgeFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new SobelEdgeDetector());
        }

        // Canny edge detector
        private void cannyEdgeFiltersItem_Click(object sender, System.EventArgs e)
        {
            CannyDetectorForm form = new CannyDetectorForm();

            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Adaptive smoothing
        private void adaptiveSmoothingFiltersItem_Click(object sender, System.EventArgs e)
        {
            AdaptiveSmoothForm form = new AdaptiveSmoothForm();

            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Conservative smoothing
        private void conservativeSmoothingFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new ConservativeSmoothing());
        }

        // Oil painting filter
        private void oilPaintingFiltersItem_Click(object sender, System.EventArgs e)
        {
            OilPaintingForm form = new OilPaintingForm();

            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Random jitter filter
        private void jitterFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Jitter(1));
        }

        // Pixellate filter
        private void pixellateFiltersItem_Click(object sender, System.EventArgs e)
        {
            PixelateForm form = new PixelateForm();

            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Simple skeletonization
        private void simpleSkeletonizationFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new SimpleSkeletonization());
        }

        // Shrink the image, removing specified color from it`s borders
        private void shrinkFiltersItem_Click(object sender, System.EventArgs e)
        {
            ShrinkForm form = new ShrinkForm();

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Conected components labeling
        private void labelingFiltersItem_Click(object sender, System.EventArgs e)
        {
            if (image.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                MessageBox.Show("Connected components labeling can be applied to binary images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            ApplyFilter(new ConnectedComponentsLabeling());
        }

        // Extract separate blobs
        private void blobExtractorFiltersItem_Click(object sender, System.EventArgs e)
        {
            if (image.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                MessageBox.Show("Blob extractor can be applied to binary images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            Blob[] blobs = BlobCounter.GetObjects(image);

            foreach (Blob blob in blobs)
            {
                host.NewDocument(blob.Image);
            }
        }

        // Resize the image
        private void ResizeImage()
        {
            ResizeForm form = new ResizeForm();

            form.OriginalSize = new Size(width, height);

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // On "Filters->Resize" menu item click
        private void resizeFiltersItem_Click(object sender, System.EventArgs e)
        {
            ResizeImage();
        }

        // Rotate the image
        private void RotateImage()
        {
            RotateForm form = new RotateForm();

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // On "Filters->Rotate" menu item click
        private void rotateFiltersItem_Click(object sender, System.EventArgs e)
        {
            RotateImage();
        }

        // Levels
        private void Levels()
        {
            LevelsLinearForm form = new LevelsLinearForm(new ImageStatistics(image,false,0,0));

            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // On "Filter->Levels" menu item click
        private void levelsFiltersItem_Click(object sender, System.EventArgs e)
        {
            Levels();
        }

        // Median filter
        private void medianFiltersItem_Click(object sender, System.EventArgs e)
        {
            ApplyFilter(new Median());
        }

        // Gamma correction
        private void gammaFiltersItem_Click(object sender, System.EventArgs e)
        {
            GammaForm form = new GammaForm();

            form.Image = image;

            if (form.ShowDialog() == DialogResult.OK)
            {
                ApplyFilter(form.Filter);
            }
        }

        // Fourier transformation
        private void ForwardFourierTransformation()
        {
            if ((!AForge.Math.Tools.IsPowerOf2(width)) ||
                (!AForge.Math.Tools.IsPowerOf2(height)))
            {
                MessageBox.Show("Fourier trasformation can be applied to an image with width and height of power of 2", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            ComplexImage cImage = ComplexImage.FromBitmap(image);

            cImage.ForwardFourierTransform();
            host.NewDocument(cImage);
        }

        // On "Filters->Fourier Transformation" click
        private void fourierFiltersItem_Click(object sender, System.EventArgs e)
        {
            ForwardFourierTransformation();
        }

        // Calculate image and screen coordinates of the point
        private void GetImageAndScreenPoints(Point point, out Point imgPoint, out Point screenPoint)
        {
            Rectangle rc = this.ClientRectangle;
            int width = (int)(this.width * zoom);
            int height = (int)(this.height * zoom);
            int x = (rc.Width < width) ? this.AutoScrollPosition.X : (rc.Width - width) / 2;
            int y = (rc.Height < height) ? this.AutoScrollPosition.Y : (rc.Height - height) / 2;

            int ix = Math.Min(Math.Max(x, point.X), x + width - 1);
            int iy = Math.Min(Math.Max(y, point.Y), y + height - 1);

            ix = (int)((ix - x) / zoom);
            iy = (int)((iy - y) / zoom);

            // image point
            imgPoint = new Point(ix, iy);
            // screen point
            screenPoint = this.PointToScreen(new Point((int)(ix * zoom + x), (int)(iy * zoom + y)));
        }

        // Normalize points so, that pt1 becomes top-left point of rectangle
        // and pt2 becomes right-bottom
        private void NormalizePoints(ref Point pt1, ref Point pt2)
        {
            Point t1 = pt1;
            Point t2 = pt2;

            pt1.X = Math.Min(t1.X, t2.X);
            pt1.Y = Math.Min(t1.Y, t2.Y);
            pt2.X = Math.Max(t1.X, t2.X);
            pt2.Y = Math.Max(t1.Y, t2.Y);
        }

        // Draw selection rectangle
        private void DrawSelectionFrame(Graphics g)
        {
            Point sp = startW;
            Point ep = endW;

            // Normalize points
            NormalizePoints(ref sp, ref ep);
            // Draw reversible frame
            ControlPaint.DrawReversibleFrame(new Rectangle(sp.X, sp.Y, ep.X - sp.X + 1, ep.Y - sp.Y + 1), Color.White, FrameStyle.Dashed);
        }

        // Crop the image
        private void Crop()
        {
            if (!cropping)
            {
                // turn on
                cropping = true;
                this.Cursor = Cursors.Cross;

            }
            else
            {
                // turn off
                cropping = false;
                this.Cursor = Cursors.Default;
            }
        }

        // On "Image->Crop" - turn on/off cropping mode
        private void cropImageItem_Click(object sender, System.EventArgs e)
        {
            Crop();
        }

        // On mouse down
        AForge.Imaging.Filters.LevelsLinear filter = new LevelsLinear();                    
        private void ImageDoc_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                // turn off cropping mode
                if (!dragging)
                {
                    cropping = false;
                    this.Cursor = Cursors.Default;
                }
                else
                {
                    
                }
            }
            else if (e.Button == MouseButtons.Left)
            {
                if (cropping)
                {
                    // start dragging
                    dragging = true;
                    // set mouse capture
                    this.Capture = true;

                    // get selection start point
                    GetImageAndScreenPoints(new Point(e.X, e.Y), out start, out startW);

                    // end point is the same as start
                    end = start;
                    endW = startW;

                    // draw frame
                    Graphics g = this.CreateGraphics();
                    DrawSelectionFrame(g);
                    g.Dispose();
                }
                else
                {
                    //code for showing the zoom window           
                    if (zoomHotSpot != null)
                    {
                        zoomHotSpot.ProcessMouseDown(e);
                    }
                    mConfiguration.RememberLastPoint = false;
                    mConfiguration.HideMouseCursor=false ;

                    mConfiguration.ZoomFactor = 3.5F;
                    Magnifier.MagnifierForm magnifier = new Magnifier.MagnifierForm(mConfiguration, new Point(e.X, e.Y));
                    zoomHotSpot = new HotSpot(new Rectangle(e.X - 2, e.Y - 2, 5, 5));
                    magnifier.Show();                    
                }
            }
        }
        private Magnifier.Configuration mConfiguration = new Magnifier.Configuration();
        protected HotSpot zoomHotSpot=null;
        protected bool zooming = false;

        // On mouse up
        private void ImageDoc_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (dragging)
            {
                // stop dragging and cropping
                dragging = cropping = false;
                // release capture
                this.Capture = false;
                // set default mouse pointer
                this.Cursor = Cursors.Default;

                // erase frame
                Graphics g = this.CreateGraphics();
                DrawSelectionFrame(g);
                g.Dispose();

                // normalize start and end points
                NormalizePoints(ref start, ref end);

                // crop tge image
                ApplyFilter(new Crop(new Rectangle(start.X, start.Y, end.X - start.X + 1, end.Y - start.Y + 1)));
            }
            else
            {
                //hide the zoomWindow
                if (zoomHotSpot != null)
                {
                    zoomHotSpot.ProcessMouseUp(e);
                }
            }
        }

        // On mouse move
        public Point mousePosition = new Point(0,0);

        private void ImageDoc_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (dragging)
            {

                Graphics g = this.CreateGraphics();

                // erase frame
                DrawSelectionFrame(g);

                // get selection end point
                GetImageAndScreenPoints(new Point(e.X, e.Y), out end, out endW);

                // draw frame
                DrawSelectionFrame(g);

                g.Dispose();

                if (SelectionChanged != null)
                {
                    Point sp = start;
                    Point ep = end;

                    // normalize start and end points
                    NormalizePoints(ref sp, ref ep);

                    SelectionChanged(this, new SelectionEventArgs(
                        sp, new Size(ep.X - sp.X + 1, ep.Y - sp.Y + 1)));
                }
            }
            else
            {
            
                if (MouseImagePosition != null)
                {
                    Rectangle rc = this.ClientRectangle;
                    int width = (int)(this.width * zoom);
                    int height = (int)(this.height * zoom);
                    int x = (rc.Width < width) ? this.AutoScrollPosition.X : (rc.Width - width) / 2;
                    int y = (rc.Height < height) ? this.AutoScrollPosition.Y : (rc.Height - height) / 2;

                    if ((e.X >= x) && (e.Y >= y) &&
                        (e.X < x + width) && (e.Y < y + height))
                    {
                        // mouse is over the image
                        mousePosition = new Point((int)((e.X - x) / zoom), (int)((e.Y - y) / zoom));
                        //Invalidate(); //repaint
                        MouseImagePosition(this, new SelectionEventArgs(mousePosition));
                    }
                    else
                    {
                        // mouse is outside image region
                        MouseImagePosition(this, new SelectionEventArgs(new Point(-1, -1)));
                    }

                    // if zoomWindow is there
                    if (zooming)
                    {
                        zoomHotSpot.ProcessMouseMove(e);                       
                    }
                }
            }
            this.Invalidate();
        }

        private void ImageDoc_MouseClick(object sender, EventArgs e)
        {
                if (MouseClickPosition != null)
                {
                        MouseClickPosition(this, new SelectionEventArgs(mousePosition));
                    }
                    else
                    {
                        // mouse is outside image region
                        MouseClickPosition(this, new SelectionEventArgs(new Point(-1, -1)));
                    }
                }
            

        // On mouse leave
        private void ImageDoc_MouseLeave(object sender, System.EventArgs e)
        {
            if ((!dragging) && (MouseImagePosition != null))
            {
                MouseImagePosition(this, new SelectionEventArgs(new Point(-1, -1)));
            }
        }

        private void ImageDoc_Load(object sender, EventArgs e)
        {
            this.ContextMenu = this.RightClickMenu;
            Graphics myGraphics = this.CreateGraphics();
            Pen myPen = new Pen(Color.Black);
            myGraphics.DrawRectangle(myPen, this.Left - 1, this.Top - 1, this.Width + 1, this.Height + 1);
            myGraphics.Dispose();

        }

        private Bitmap TransverseSlice(int zslice)
        {
            Bitmap temp;
            temp = new Bitmap(this.x, this.y, PixelFormat.Format24bppRgb);
            for (int i = 0; i < this.x; i++)
                for (int j = 0; j < this.y; j++)
                    temp.SetPixel(i, j, Color.FromArgb(this.datamatrix[i, j, zslice], this.datamatrix[i, j, zslice], this.datamatrix[i, j, zslice]));
            return temp;

        }

        private Bitmap CoronalSlice(int yslice)
        {
            Bitmap temp;
            temp = new Bitmap(this.x, this.z,PixelFormat.Format24bppRgb);
            for (int i = 0; i < this.x; i++)
                for (int j = 0; j < this.z; j++)
                    temp.SetPixel(i, this.z - 1 - j, Color.FromArgb(this.datamatrix[i, yslice, j], this.datamatrix[i, yslice, j], this.datamatrix[i, yslice, j]));
            return temp;

        }

        private Bitmap SagittalSlice(int xslice)
        {
            Bitmap temp;
            temp = new Bitmap(this.y, this.z, PixelFormat.Format24bppRgb);
            for (int i = 0; i < this.y; i++)
                for (int j = 0; j < this.z; j++)
                    temp.SetPixel(this.y - 1 - i, this.z - 1 - j, Color.FromArgb(this.datamatrix[this.x - 1 - xslice, i, j], this.datamatrix[this.x - 1 - xslice, i, j], this.datamatrix[this.x - 1 - xslice, i, j]));
            return temp;

        }
        
        public void ChangeCurrentslice(string type, int slice)
        {
            // Release current image
            image.Dispose();
            // set document image to just loaded


            if (type == "s")
            {
                sbar.Value = slice;
                if (this.IsColorMatrixLoaded == true)
                    this.image = CMatrix.SagittalSlice(sbar.Value);
                else
                    this.image = SagittalSlice(sbar.Value);

            }
            if (type == "t")
            {
                tbar.Value = slice;
                if (this.IsColorMatrixLoaded == true)
                    this.image = CMatrix.TransverseSlice(tbar.Value);
                else
                    this.image = TransverseSlice(tbar.Value);

            }
            if (type == "c")
            {
                cbar.Value = slice;
                if (this.IsColorMatrixLoaded == true)
                    this.image = CMatrix.CoronalSlice(cbar.Value);
                else
                    this.image = CoronalSlice(cbar.Value);

            }
            // format image
            AForge.Imaging.Image.FormatImage(ref image);
            UpdateNewImage();
            UpdateSize();
            UpdateZoom();
            this.currentType= type;
        }
string currentType ;

        private void tbar_Scroll(object sender, EventArgs e)
        {
            // Release current image
            image.Dispose();

            this.currentType = "t";
            // set document image to just loaded

            if (this.IsColorMatrixLoaded == true)
                this.image = CMatrix.TransverseSlice(tbar.Value);
            else
                this.image = TransverseSlice(tbar.Value);

            // format image
            AForge.Imaging.Image.FormatImage(ref image);
            UpdateNewImage();
            UpdateSize();
            UpdateZoom();
            slice("t" + tbar.Value, EventArgs.Empty);
        }

        private void menuItem13_Click(object sender, EventArgs e)
        {
            menuItem13.Checked = !menuItem13.Checked;
        }


        private void menuItem32_Click(object sender, EventArgs e)
        {
            menuItem32.Checked = !menuItem32.Checked;
        }

        private void menuItem36_Click(object sender, EventArgs e)
        {
            if (menuItem36.Checked)
            {
                menuItem37.Checked = false;
                menuItem38.Checked = false;
            }
            menuItem36.Checked = !menuItem36.Checked;
        }

        private void menuItem37_Click(object sender, EventArgs e)
        {
            if (menuItem37.Checked)
            {
                menuItem36.Checked = false;
                menuItem38.Checked = false;
            }
            menuItem37.Checked = !menuItem37.Checked;
        }

        private void menuItem38_Click(object sender, EventArgs e)
        {
            if (menuItem38.Checked)
            {
                menuItem37.Checked = false;
                menuItem36.Checked = false;
            }
            menuItem38.Checked = !menuItem38.Checked;
        }

        private void menuItem39_Click(object sender, EventArgs e)
        {
            if (menuItem39.Checked)
            {
                menuItem40.Checked = false;
                menuItem41.Checked = false;
            }
            menuItem39.Checked = !menuItem39.Checked;
        }

        private void menuItem40_Click(object sender, EventArgs e)
        {
            if (menuItem40.Checked)
            {
                menuItem39.Checked = false;
                menuItem41.Checked = false;
            }
            menuItem40.Checked = !menuItem40.Checked;
        }

        private void menuItem41_Click(object sender, EventArgs e)
        {
            if (menuItem41.Checked)
            {
                menuItem40.Checked = false;
                menuItem39.Checked = false;
            }
            menuItem41.Checked = !menuItem41.Checked;
        }

        public float VScrollBarValue, HScrollBarValue;

        private void ImageDoc_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
                this.HScrollBarValue = e.NewValue;
            else
                this.VScrollBarValue = e.NewValue;

            this.ScrollChanged(null, EventArgs.Empty);

        }

        public event EventHandler ShowSubjectInfo;

        private void menuItem43_Click(object sender, EventArgs e)
        {
            MessageBox.Show("FOV: " + this.fov.ToString() + "\n" + "Origin: " + this.center.ToString() + "\n");
            this.ShowSubjectInfo(sender, e);
        }

        public event EventHandler DelThis,DelBefore,DelAfter;

        private void deleteThisSliceToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (this.IsColorMatrixLoaded == true)
            {
                MessageBox.Show("Selected action cannot be done on color matrices.");
                return;
            }
            DelThis("T" + tbar.Value.ToString(),EventArgs.Empty);

        }

        private void deleteSlicesBeforeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.IsColorMatrixLoaded == true)
            {
                MessageBox.Show("Selected action cannot be done on color matrices.");
                return;
            }
            DelBefore("T" + tbar.Value.ToString(), EventArgs.Empty);

        }

        private void deleteSlicesAfterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.IsColorMatrixLoaded == true)
            {
                MessageBox.Show("Selected action cannot be done on color matrices.");
                return;
            }
            DelAfter("T" + tbar.Value.ToString(), EventArgs.Empty);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (this.IsColorMatrixLoaded == true)
            {
                MessageBox.Show("Selected action cannot be done on color matrices.");
                return;
            }
            DelThis("C" + cbar.Value.ToString(), EventArgs.Empty);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (this.IsColorMatrixLoaded == true)
            {
                MessageBox.Show("Selected action cannot be done on color matrices.");
                return;
            }
            DelBefore("C" + cbar.Value.ToString(), EventArgs.Empty);
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if (this.IsColorMatrixLoaded == true)
            {
                MessageBox.Show("Selected action cannot be done on color matrices.");
                return;
            }
            DelAfter("C" + cbar.Value.ToString(), EventArgs.Empty);
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            if (this.IsColorMatrixLoaded == true)
            {
                MessageBox.Show("Selected action cannot be done on color matrices.");
                return;
            }
            DelThis("S" + sbar.Value.ToString(), EventArgs.Empty);
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            if (this.IsColorMatrixLoaded == true)
            {
                MessageBox.Show("Selected action cannot be done on color matrices.");
                return;
            }
            DelBefore("S" + sbar.Value.ToString(), EventArgs.Empty);
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            if (this.IsColorMatrixLoaded == true)
            {
                MessageBox.Show("Selected action cannot be done on color matrices.");
                return;
            }
            DelAfter("S" + sbar.Value.ToString(), EventArgs.Empty);
        }

        public double ScaleConstant, InterceptConstant;
        private void toolStripButton3_Click(object sender, EventArgs e)
        {

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel/Matlab Tabbed Sheet(*.csv)|*.csv|All files (*.*)|*.*";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(sfd.FileName);
                System.IO.StreamWriter tex = fi.CreateText();
                String line;
                for (int i = 0; i < image.Height; i++)
                {
                    line = "";
                    for (int j = 0; j < image.Width; j++)
                        if (j != image.Width - 1)
                            line = line + ((((double)image.GetPixel(j, i).R) / ScaleConstant) + InterceptConstant) + ",";
                        else
                            line = line + ((((double)image.GetPixel(j, i).R) / ScaleConstant) + InterceptConstant);
                    tex.WriteLine(line);
                }
                MessageBox.Show("Done!");
                tex.Close();

            }
        }

        private void menuItem47_Click(object sender, EventArgs e)
        {
            WinForm a = new WinForm(image);
            a.Show();
        }

        public bool HotTracking = true;
        private void menuItem49_Click(object sender, EventArgs e)
        {
            menuItem49.Checked = !menuItem49.Checked;
            HotTracking = menuItem49.Checked;
        }

        private void cbar_Scroll(object sender, EventArgs e)
        {
            this.currentType = "c";
            // Release current image
            image.Dispose();
            // set document image to just loaded	
            if (this.IsColorMatrixLoaded == true)
                this.image = CMatrix.CoronalSlice(cbar.Value);
            else
                this.image = CoronalSlice(cbar.Value);

            // format image
            AForge.Imaging.Image.FormatImage(ref image);
            UpdateSize();
            UpdateZoom(); ;
            UpdateNewImage();
            slice("c" + cbar.Value, EventArgs.Empty);

        }

        private void sbar_Scroll(object sender, EventArgs e)
        {
            this.currentType = "s";
            // Release current image
            image.Dispose();
            // set document image to just loaded
            if (this.IsColorMatrixLoaded == true)
                this.image = CMatrix.SagittalSlice(sbar.Value);
            else
                this.image = SagittalSlice(sbar.Value);
            // format image
            AForge.Imaging.Image.FormatImage(ref image);
            UpdateSize();
            UpdateZoom();
            UpdateNewImage();
            slice("s" + sbar.Value, EventArgs.Empty);

        }


        private void cbar_ValueChanged(object sender, EventArgs e)
        {
            // Release current image
            image.Dispose();
            // set document image to just loaded	
            if (this.IsColorMatrixLoaded == true)
                this.image = CMatrix.CoronalSlice(cbar.Value);
            else
                this.image = CoronalSlice(cbar.Value);
            // format image
            AForge.Imaging.Image.FormatImage(ref image);
            UpdateSize();
            UpdateZoom(); ;
            UpdateNewImage();
            slice("c" + cbar.Value, EventArgs.Empty);

        }

        private void tbar_ValueChanged(object sender, EventArgs e)
        {
            // Release current image
            image.Dispose();
            // set document image to just loaded	
            if (this.IsColorMatrixLoaded == true)
                this.image = CMatrix.TransverseSlice(tbar.Value);
            else
                this.image = TransverseSlice(tbar.Value);
            // format image
            AForge.Imaging.Image.FormatImage(ref image);
            UpdateSize();
            UpdateZoom(); ;
            UpdateNewImage();
            slice("t" + tbar.Value, EventArgs.Empty);

        }

        private void sbar_ValueChanged(object sender, EventArgs e)
        {
            // Release current image
            image.Dispose();
            // set document image to just loaded
            if (this.IsColorMatrixLoaded == true)
                this.image = CMatrix.SagittalSlice(sbar.Value);
            else
                this.image = SagittalSlice(sbar.Value);
            // format image
            AForge.Imaging.Image.FormatImage(ref image);
            UpdateSize();
            UpdateZoom();
            UpdateNewImage();
            slice("s" + sbar.Value, EventArgs.Empty);
        }

        private void menuItem52_Click(object sender, EventArgs e)
        {
            ///save as xml
            MatrixDataStructures.SerializeMatrixtoXML FileSaveObject = new SerializeMatrixtoXML();
            MatrixDataStructures.SingleVolume TempVolume = new SingleVolume();
                    

            TempVolume.Center = new CoordinateD(0,0,0);
            TempVolume.FOV = new CoordinateD(this.x,this.y, this.z);
            TempVolume.HeaderFileName = "NA";
            TempVolume.Matrix = new Matrix3DInt16(datamatrix);

            FileSaveObject.SaveAs(TempVolume);        
            
        }

        private void menuItem53_Click(object sender, EventArgs e)
        {
            /// save as analyze
            /// 
            if ((fov == null) || (center == null))
            {
                MessageBox.Show("Insufficient Header Information");
                return;
            }
            System.Windows.Forms.SaveFileDialog SFD = new System.Windows.Forms.SaveFileDialog();

            SFD.AddExtension = false;
            SFD.Title = "Save Analyze file as ...";
            SFD.Filter = "All files (*.*)|*.*";
            if (SFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                MatrixDataStructures.SingleVolume TempVolume = new SingleVolume();

                if (MessageBox.Show("Preserve Center information (" + center.x.ToString() + "," + center.y.ToString() + "," + center.z.ToString() + ") ?", "Warning", MessageBoxButtons.YesNo) != DialogResult.Yes)
                { TempVolume.Center = new CoordinateD(0, 0, 0); }
                else
                { TempVolume.Center = center; }
                
                TempVolume.FOV = fov;
                TempVolume.HeaderFileName = "NA";
                TempVolume.Matrix = new Matrix3DInt16(datamatrix);
                HdrImgWriter.WriteToFile(TempVolume, SFD.FileName);
            }
        }

        private void menuItem55_Click(object sender, EventArgs e)
        {
            menuItem55.Checked = !menuItem55.Checked;
            if (menuItem55.Checked)
            {
                menuItem56.Checked = false;
                menuItem57.Checked = false;
                menuItem58.Checked = false;
                menuItem59.Checked = false;
            }

        }
            private void menuItem56_Click(object sender, EventArgs e)
        {
            menuItem56.Checked = !menuItem56.Checked;
            if (menuItem56.Checked)
            {
                menuItem55.Checked = false;
                menuItem57.Checked = false;
                menuItem58.Checked = false;
                menuItem59.Checked = false;
            }
        }

        private void menuItem57_Click(object sender, EventArgs e)
        {
            menuItem57.Checked = !menuItem57.Checked;
            if (menuItem57.Checked)
            {
                menuItem56.Checked = false;
                menuItem55.Checked = false;
                menuItem58.Checked = false;
                menuItem59.Checked = false;
            }
        }

        private void menuItem58_Click(object sender, EventArgs e)
        {
            menuItem58.Checked = !menuItem58.Checked;
            if (menuItem58.Checked)
            {
                menuItem56.Checked = false;
                menuItem57.Checked = false;
                menuItem55.Checked = false;
                menuItem59.Checked = false;
            }
        }

        private void menuItem59_Click(object sender, EventArgs e)
        {
            menuItem59.Checked = !menuItem59.Checked;
            if (menuItem59.Checked)
            {
                menuItem56.Checked = false;
                menuItem57.Checked = false;
                menuItem58.Checked = false;
                menuItem55.Checked = false;
            }
        }

        private void menuItem60_Click(object sender, EventArgs e)
        {
            System.IO.Directory.CreateDirectory("visualization_images");
            twoDMatrixLibrary.Matrix slice = new twoDMatrixLibrary.Matrix(this.y, this.x);
            Bitmap temp;
            for (int i=0;i < this.z;i++)
            {
                string filename = "visualization_images\\image." + (i+1);
                for (int j = 0; j < this.x; j++)
                    for (int k = 0; k < this.y; k++)
                        slice[k, j] = datamatrix[j, k, i];

                temp = slice.ToBitmap();
                temp.Save(filename,ImageFormat.Png);
            }
           
        }

        private void menuItem60_Click_1(object sender, EventArgs e)
        {
            string tempfilename = System.IO.Path.GetTempFileName();
            MetaView3D.WriteVTK writer = new MetaView3D.WriteVTK();
            writer.Save(this.datamatrix,tempfilename);
            MessageBox.Show("VTK file saved as:" + tempfilename,"VTK File", MessageBoxButtons.OK);
            MetaView3D.Form2 loader = new MetaView3D.Form2();
            loader.Show();
        }

        private void ImageDoc_Closed(object sender, FormClosedEventArgs e)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        private void menuItem61_Click(object sender, EventArgs e)
        {
            autoFOV.Checked = !autoFOV.Checked;
        }
        
        private void ImageDoc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Shift)
            {
                if (filter.InRed.Max > 2)
                {
                    Range r = new Range(0, filter.InRed.Max - 1);
                    filter.InRed = r;
                    filter.InBlue = r;
                    filter.InGreen = r;
                    filter.Input = r;
                    //Refresh();
                }
                else
                    MessageBox.Show(filter.InRed.Max.ToString());
            }
        }


        public Bitmap contrast_Stretch(Bitmap bmpimg, float max, float min)
        {
            BitmapData data = bmpimg.LockBits(new Rectangle(0, 0, bmpimg.Width, bmpimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            unsafe
            {
                byte* ptr = (byte*)data.Scan0;

                int remain = data.Stride - data.Width * 3;

                max = 0;
                min = 255;

                for (int i = 0; i < data.Height; i++)
                {
                    for (int j = 0; j < data.Width; j++)
                    {
                        if (min > ptr[0])
                            min = ptr[0];

                        if (max < ptr[0])
                            max = ptr[0];


                        ptr += 3;
                    }
                    ptr += remain;
                }

                ptr = (byte*)data.Scan0;

                for (int i = 0; i < data.Height; i++)
                {
                    for (int j = 0; j < data.Width; j++)
                    {
                        ptr[0] = ptr[1] = ptr[2] = (byte)((ptr[0] - min) * (255 / (max - min)));

                        ptr += 3;
                    }
                    ptr += remain;
                }

            }

            bmpimg.UnlockBits(data);
            return bmpimg;
        }

        private void RightClickMenu_Popup(object sender, EventArgs e)
        {

        }

        private void menuItem61_Click_1(object sender, EventArgs e)
        {
            menuItem61.Checked = !menuItem61.Checked;
            this.Invalidate();
        }

    }

    // Selection arguments
    public class SelectionEventArgs : EventArgs
    {
        private Point location;
        private Size size;

        // Constructors
        public SelectionEventArgs(Point location)
        {   
            this.location = location;
        }
        public SelectionEventArgs(Point location, Size size)
        {
            this.location = location;
            this.size = size;
        }

        // Location property
        public Point Location
        {
            get { return location; }
        }
        // Size property
        public Size Size
        {
            get { return size; }
        }
    }

    public class FormStateEventArgs : EventArgs
    {
        private float Zoom, VScrollValue, HScrollValue;
        public float zoom
        {
            get { return Zoom; }
        }
        public float VerticalScrollValue
        {
            get { return VScrollValue; }
        }
        public float HorizontalScrollValue
        {
            get { return HScrollValue; }
        }

        public FormStateEventArgs(float Zoom)
        {
            this.Zoom = Zoom;
        }
        public FormStateEventArgs(float Zoom, float HScroll, float VScroll)
        {
            this.Zoom = Zoom;
            this.HScrollValue = HScroll;
            this.VScrollValue = VScroll;
        }

    }

    // Commands
    public enum ImageDocCommands
    {
        Clone,
        Crop,
        ZoomIn,
        ZoomOut,
        ZoomOriginal,
        FitToSize,
        Levels,
        Grayscale,
        Threshold,
        Morphology,
        Convolution,
        Resize,
        Rotate,
        Brightness,
        Contrast,
        Saturation,
        Fourier
    }

        